Monday, 17 November 2014

C++ mutable keyword with example scenarios

C++ mutable keyword

Mutable is also one of the storage class specifier in C++, along 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 we need mutable keyword? What is the use of Mutable Keyword?

Sometimes there is 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.
Mutable keyword is used to perform the modifications on the const variables.

Below is the example where 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 name and the price. At this scenario, we have to create the variable with the storage class mutable.

#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:
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 above program. The values of pcchOwner and iPrice data members are changed from const function because they are declared as mutable.

The mutable keyword 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 object declared as const.

NOTE: You cannot use the mutable keyword with names declared as static or const, or reference.

No comments: