Is it possible to initialize private const member variables in a constructor?
Printable View
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_;
};
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.