tags:

views:

175

answers:

5

So how i can do this? So that no member function can change the value of its data members once object has been initialized in C++.

+6  A: 

Declare the members as const.

Thomas
You mean the member functions, right?
Ed Swangren
Personally I pressed upvote for "members", not "member functions"
Pavel Shved
Initialization gets really messy if complex for const members. Chacun a son goat.
bmargulies
That is ridiculous. You cannot make every member variable const. There is nothing wrong with changing state internally and exposing functions which should not change state as 'const'.
Ed Swangren
And that does not answer the intent of the question. The OP wants a contract that says "I am a function that will not change the internal state of this object", not "You have no idea what I may change, but don't worry about it; my private members are const".
Ed Swangren
@Ed, if you need an invariant then it should be declared as const so that people don't accidentally modify it. This is what I believe the op is asking about. If the member really is used for tracking state then it shouldn't be const. Pick the right tool for the job and all that...
Glen
@Glen: Sure, that's right. I think that I misunderstood the question. I took the title "Disallow any member function to change its data members in C++ Class" to mean "how do I create a member function that cannot modify any members." I think you were correct and I was wrong, +1
Ed Swangren
@Ed, I think the wording of the question is a little ambiguous as different people have interpreted it differently.
Glen
+13  A: 

Make all the member functions const. That's the only mechanism for the job, and it works just fine. If you also make them private you're completely covered.

If for some reason you feel compelled to mark them protected, then things are more complicated.

You will need to make the individual fields const, and that will in turn require you to initialize them via the member initialization list, or a const_cast of this in the constructor. Or maybe a mutable ctor, but I'm not sure there is such a thing.

bmargulies
thanks, Oops why i did not get it first loz?
itsaboutcode
Because C++ uses 'const' to mean too many slightly different things.
bmargulies
Incorrect. A class that inherits the given one will be capable of changing members via member functions.
Pavel Shved
@Pavel Shved, the OP didn't say, 'so nothing could change it,' the OP wrote 'so that no member functions can ...' It's reasonable to interpret this as 'no member functions of the class'.
bmargulies
@Pavel Incorrect.
anon
@Neil i dunno, I'd call it pendantic but not incorrect. Anyway, I hope that me edit satisfies all.
bmargulies
@bmargulies A derived class does not get any special privileges. If a member function is const then it is still const if used by a derived class.
anon
@Neil, derived class can change protected/public members via *its own* member functions, not inherited ones!
Pavel Shved
@Neil isn't his concern about protected: members modified by derived classes? More edit needed.
bmargulies
If you are talking about protected data members, then this is a well known code smell.
anon
@Neil, just because it's a code smell doesn't mean it's illegal syntax, or wrong in all cases
Glen
It's also worth noting that it's not 100% possible to do this in C++. A particularily evil developer could always write directly to the memory offset of the member variable.
Glen
anon
+1  A: 

As a suggestion, the methods should be suffixed with const since they won't change the members (data). This may be redundant, but it gives notification to the users of this class.

Derived classes can only change members of the parent's (superclass) if the access is marked as protected or public and the members are not const. However, there are heroic methods for altering data using pointers and casting, but the principle is not to modify const objects.

Thomas Matthews
+2  A: 

You could simply declare the instance of the class that you are interested in keeping unchanged as const:

const Person p( "fred" );
anon
A: 

The way to do this is by declaring the method(s) as const, like this:

void f () const;

However, it's important to note that members marked as mutable may still be changed - even through const methods. For more info about the mutable keyword you can read this.

rmn