Prevent member variable from being modified after construction
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:
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
};
The code executes. I'm just curious if there is someway to make the accidential reassignment in testMember() illegal.
Thanks.