I'm an OOP newbie and just learning classes. Why can't I create constants and use them in classes without the static specifier? Like this:
class MyClass{
private:
const int MyConst = 10;
int MyArr[MyConst];
};
The compiler complains that:
invalid use of non-static data member ‘MyClass::MyConst’
'MyArr' was not declared in this scope
I checked out some tutorials, like C++ Primer Plus, which tells me to create an enumeration. Why do enumerations work? Don't they create constants like using the const qualifier?
int MyArr[MyConst];would make no sense because all class instances must have the same size known at compilation time.MyConstwaspublicand notprivate. You could then create an object ofMyClassthat initializesMyConstto a different value (even if it was declaredconst int). Or, you could define a constructor that initializesMyConstto a different value. C++ doesn't support dynamic C-style arrays in class declarations.