21.3. mutable Class Members

In Section 21.2, we introduced the const_cast operator, which allowed us to remove the “const-ness” of a type. A const_cast operation can also be applied to a data member of a const object from the body of a const member function of that object’s class. This enables the const member function to modify the data member, even though the object is considered to be const in the body of that function. Such an operation might be performed when most of an object’s data members should be considered const, but a particular data member still needs to be modified.

As an example, consider a linked list that maintains its contents in sorted order. Searching through the linked list does not require modifications to the data of the ...

Get C++11 for Programmers, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.