mutable Class Members
In Section 24.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 linked list, so the search function could be a const member function of the linked-list class. However, it is conceivable that a linked-list object, in an effort to make future searches more efficient, might keep track of the location of the last successful match. If the next search operation attempts to locate an item that appears later in the list, the search could begin from the location of the last successful match, rather than from the beginning of the list. To do this, the const member function that performs the search must be able to modify the data member that keeps track of the last successful search.
If a data member such as the one described above should always be modifiable, C++ provides the storage-class specifier mutable as an alternative to const_cast. A mutable data member is always modifiable, even in a const member function or const object. This reduces the need to cast away "const-ness."
Portability Tip 24.1
The effect of attempting to modify an object that was defined as constant, regardless of whether that modification was made possible by a const_cast or C-style cast, varies among compilers. |
Both mutable and const_cast allow a data member to be modified; they are used in different contexts. For a const object with no mutable data members, operator const_cast must be used every time a member is to be modified. This greatly reduces the chance of a member being accidentally modified because the member is not permanently modifiable. Operations involving const_cast are typically hidden in a member function's implementation. The user of a class might not be aware that a member is being modified.
Software Engineering Observation 24.3
mutable members are useful in classes that have "secret" implementation details that do not contribute to the logical value of an object. |
Mechanical Demonstration of a mutable Data Member
Figure 24.5 demonstrates using a mutable member. The program defines class TestMutable (lines 822), which contains a constructor, function getValue and a private data member value that is declared mutable. Lines 1619 define function getValue as a const member function that returns a copy of value. Notice that the function increments mutable data member value in the return statement. Normally, a const member function cannot modify data members unless the object on which the function operatesi.e., the one to which this pointsis cast (using const_cast) to a non-const type. Because value is mutable, this const function is able to modify the data.
Figure 24.5. Demonstrating a mutable data member.
(This item is displayed on pages 1210 - 1211 in the print version)
1 // Fig. 24.5: fig24_05.cpp 2 // Demonstrating storage-class specifier mutable. 3 #include 4 using std::cout; 5 using std::endl; 6 7 // class TestMutable definition 8 class TestMutable 9 { 10 public: 11 TestMutable( int v = 0 ) 12 { 13 value = v; 14 } // end TestMutable constructor 15 16 int getValue() const 17 { 18 return value++; // increments value 19 } // end function getValue 20 private: 21 mutable int value; // mutable member 22 }; // end class TestMutable 23 24 int main() 25 { 26 const TestMutable test( 99 ); 27 28 cout << "Initial value: " << test.getValue(); 29 cout << " Modified value: " << test.getValue() << endl; 30 return 0; 31 } // end main
|
Line 26 declares const TestMutable object test and initializes it to 99. Line 28 calls the const member function getValue, which adds one to value and returns its previous contents. Notice that the compiler allows the call to member function getValue on the object test because it is a const object and getValue is a const member function. However, getValue modifies variable value. Thus, when line 29 invokes getValue again, the new value (100) is output to prove that the mutable data member was indeed modified.