There is a class

class A { private: const int _NUM = 20; //... public: A(); //... }; A::A() { char arrTemp[_NUM]; //... } 

And the message that this can not be used in a constant expression and when trying to compile the error is added "The expression is not defined by a constant." The compiler reads the class definition from the header file and finds out that _NUM is a const int.

Please explain why it is impossible to do this.

    1 answer 1

    Formally speaking, you can redefine, say, A::A():_NUM(5) . So this is not something that is not a constant ... But this is not exactly a constant - it can be redefined in the constructor. You simply say that after it is defined in the constructor , it will not change. And 20 is just the value that it has, if the constructor does not override it.

    Now if you write

     class A { private: static const int _NUM = 20; 

    then the matter is completely different.