Possible Duplicate:
What is the meaning of a const at end of a member function?
If my class definition is as follows:
type CLASS::FUNCTION(int, const char*) const
What does the last const after the closing bracket mean, and how do I apply it to the function:
type CLASS::FUNCTION(int var1, const char* var2) {
}
It means that this function does not modify the observable state of an object.
In compiler terms, it means that you cannot call a function on a
constobject (or const reference or const pointer) unless that function is also declared to beconst. Also, methods which are declaredconstare not allowed to call methods which are not.Update: as Aasmund totally correctly adds,
constmethods are allowed to change the value of members declared to bemutable.For example, it might make sense to have a read-only operation (e.g.
int CalculateSomeValue() const) which caches its results because it's expensive to call. In this case, you need to have amutablemember to write the cached results to.I apologize for the omission, I was trying to be fast and to the point. :)