Possible Duplicate:
Is there a reason to use enum to define a single constant in C++ code?
I just came across the following snippet in some old code, with an odd use of enum:-
class MyClass
{
public:
enum {MAX_ITEMS=16};
int things[MAX_ITEMS];
...
} ;
This is better than #define MAX_ITEMS 16 but is it any different from static const int MAX_ITEMS=16;?
Digging back into the mists of memory, I remember some C++ compilers not allowing you to initialize consts within the class, instead requiring a separate…
const int MyClass::MAX_ITEMS = 16;
… in the .cpp source file. Is this just an old workaround for that?
This is the age old “enum hack“ used to initialize arrays inside the class definition.
Traditionally, pre C++03 it was not possible to initialize a
staticconstinside the class declaration. Since array declaration needs a compile time constant index in declaration. The enum hack was used as an workaround.