Constant class member functions

suggest change

Versions

Remarks

What does a const member function of a class really mean? A const member function cannot modify the object it is called on. That includes assigning to a non-mutable data member and calling a non-const member function.

It can still modify an object reached through a pointer or reference member, as in the example. The pointer member itself is unchanged, so writing into the map is well-formed. That still changes the program’s logical state. A mutable member may also be assigned.

delete on that pointer is well-formed in a const function, but it leaves the pointer dangling. Assigning a new pointer to the member is ill-formed.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents