I often find myself wishing I could have an object's member variable be const, but that the system allowed initialization of that const variable after construction. Is there a mechanism that would allow me to do this?
To clarify, here is an example:
class A
{
public:
A(){}
initialize(int x) { c = x; }
private:
const int c;
}
I want to be able to do something like that. I don't have this information at construction, so I can't simply move initialization to the initialization list of the constructor.
No, you cannot initialize const member after contruction.
Do not forget, however, that you can call static functions in initializer list, so in most of cases you can initialize memebers from initializer list
You can also define special getter for that member and use it to access member.