C++ mutable keyword
Mutable is also one of the storage class specifier in C++, along with the mutable, there are auto, register, static and extern are the storage class specifiers in C++. There is another storage class specifier in C is typedef.
Why do we need mutable keywords? What is the use of Mutable Keyword?
Sometimes there is a requirement to modify one or more data members of class/struct through const function even though you don’t want the function to update other members of class/struct.
A mutable keyword is used to perform the modifications on the const variables.
Below is the example where the use of mutable can be useful. Suppose we have the vehicle class, the features of the vehicle are same where ever you go. Suppose we want to sell the vehicle, we need to change the owner’s name and the price. At this scenario, we have to create the variable with the storage class mutable.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
#include <iostream> #include <string.h> using namespace std; class MyVehicleCalss { char pcchOwner[25]; mutable char pcchNewOwner[50]; int iNum; mutable int iPrice; public: MyVehicleCalss (char* s, char* m, int a, int p) { strcpy (pcchOwner, s); strcpy (pcchNewOwner, m); iNum = a; iPrice = p; } void ChangeOwner (char* pcOrder) const { strcpy (pcchNewOwner, pcOrder); } void ChangePrice (int iVal) const { iPrice = iVal; } void ShowDetails () const { cout << "MyVehicleCalss pcchOwner is: " << pcchOwner << endl; cout << "Food ordered by MyVehicleCalss is: " << pcchNewOwner << endl; cout << "Car no is: " << iNum << endl; cout << "Total payable amount: " << iPrice << endl; } }; int _tmain (int argc, _TCHAR* argv[]) { cout << " Initializing the Vehicle class" << endl; const MyVehicleCalss ocMyCls("Tarah", "Ice Cream", 5, 500000); ocMyCls.ShowDetails (); cout << " Changing the order" << endl; ocMyCls.ChangeOwner ("Suve"); cout << " Changing the price" << endl; ocMyCls.ChangePrice (200000); ocMyCls.ShowDetails (); getchar (); return 0; } |
The output of the above program is:
1 2 3 4 5 6 7 8 9 10 11 |
Initializing the Vehicle class MyVehicleCalss pcchOwner is: Tarah Food ordered by MyVehicleCalss is: Ice Cream Car no is: 5 Total payable amount: 500000 Changing the order Changing the price MyVehicleCalss pcchOwner is: Tarah Food ordered by MyVehicleCalss is: Suve Car no is: 5 Total payable amount: 200000 |
Closely observe the output of the above program. The values of pcchOwner and iPrice data members are changed from const function because they are declared as mutable.
The mutable keyword is mainly used to allow the programmer to modify the const data member to the modified one. Adding mutable to a variable allows a const pointer to change members.
mutable is particularly useful if most of the members should be constant but a few need to be updateable. Data members declared as mutable can be modified even though they are the part of an object declared as const.