I am trying to define a constant BUFFER_LENGTH for my class for a given usecase.
//1. Using preprocessor declaration
//#define BUFFER_LENGTH 12
//2.Global constant
//const int BUFFER_LENGTH = 12;
class MyRequest
{
public:
//3. Define an in-class constant
//static const int BUFFER_LENGTH = 12;
//4. Declare an enum constant
enum
{
BUFFER_LENGTH = 12
};
MyRequest()
{
strcpy(mBuffer, "TestString");
printf("Buffer: %s, BUFFER_LENGTH = %d",mBuffer, BUFFER_LENGTH);
}
private:
char mBuffer[BUFFER_LENGTH];
};
I just listed down the different ways in which constant can be defined for the class.
1. Using Preprocessor constant
2. Using Global constant
3. Using in-class constant
4. using an enum.
Out of these, which is the best approach to define the constants for the given use case? I prefer to use the enum constant over others approaches. Is there any other better approach which I have missed out.
Thanks,