The new storage class specifier mutable allows you to declare class member
data as 'modifiable even in a const object'. This allows you to distinguish
between 'abstract const' (const to the user) and 'concrete const'
(const to
the implementation).
An example makes this clearer:
class Object { public: void f(); void g() const; private: int m1; mutable int m2; }; void Object::f() // can always modify object { m1 = 0; m2 = 1; } void Object::g() const // object is const { // cannot modify m1 m2 = 2; // can modify m2 because // it is mutable }
A realistic example is a class that performs an expensive calculation whose value is returned by a const member function. That function can now cache the value in a mutable data member to save doing the calculation each time (in fact you'd need two data members: the cache itself and a mutable flag indicating whether the cache is valid).