Is it possible to initialize private const member variables in a constructor?
Is it possible to initialize private const member variables in a constructor?
Couldn't you just initialize it in the classes private field
Code:class name { private: const typename name = _value_; };
Yes.
Code:class Foo { public: Foo() : n_(0) {} private: const int n_; };
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I remember I did that once and for a minute it was telling me "ISO C++ didn't allow initialization of Constant members" and I restarted and it compiled fine. it was in DevC++ beta 5.
Initialization of constant members is only allowed in an initialization list, if it's done in the body of the constructor you'll get an error.
I meant I initialized it in the private sector of the class.