I've ran into situations once or twice where the class constructor sets a member variable, after which it should not be modified.
Is there a way of getting a member to behave as if it were const, but only after the constructor runs?
Some example code:
The code executes. I'm just curious if there is someway to make the accidential reassignment in testMember() illegal.Code:class Foo { public: Foo(int in) { member = in; // initial assignment in constructor is legal }; void testMember() { if(member = 1); // accidental assignment should be illegal //do something // legitimately modify other class data besides member }; private: int member; // should not be modified by any function besides constructor };
Thanks.



LinkBack URL
About LinkBacks




. It just seemed to obvious!