The mutable storage class specifier in C++ (or use of mutable keyword in C++)
auto, register, static and extern are the storage class specifiers in C. typedef is also considered as a storage class specifier in C. C++ also supports all these storage class specifiers. In addition to this C++, adds one important storage class specifier whose name is mutable.
What is the need of mutable?
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. This task can be easily performed by using mutable keyword. Consider this example where use of mutable can be useful. Suppose you go to hotel and you give the order to waiter to bring some food dish. After giving order, you suddenly decide to change the order of food. Assume that hotel provides facility to change the ordered food and again take the order of new food within 10 minutes after giving the 1st order. After 10 minutes order can’t be cancelled and old order can’t be replaced by new order. See the following code for details.
C++
#include <bits/stdc++.h>
#include <string.h>
using namespace std;
// Customer Class
class Customer {
// class Variables
string name;
mutable string placedorder;
int tableno;
mutable int bill;
// member methods
public:
// constructor
Customer(string s, string m, int a, int p)
{
name= s;
placedorder=m;
tableno = a;
bill = p;
}
// to change the place holder
void changePlacedOrder(string p) const
{
placedorder=p;
}
// change the bill
void changeBill(int s) const { bill = s; }
// to display
void display() const
{
cout << "Customer name is: " << name << endl;
cout << "Food ordered by customer is: "
<< placedorder << endl;
cout << "table no is: " << tableno << endl;
cout << "Total payable amount: " << bill << endl;
}
};
// Driver code
int main()
{
const Customer c1("Pravasi Meet", "Ice Cream", 3, 100);
c1.display();
c1.changePlacedOrder("GulabJammuns");
c1.changeBill(150);
c1.display();
return 0;
}
OutputCustomer name is: Pravasi Meet
Food ordered by customer is: Ice Cream
table no is: 3
Total payable amount: 100
Customer name is: Pravasi Meet
Food ordered by customer is: GulabJammuns
table no is: 3
Total payable amount: 150
Closely observe the output of above program. The values of placedorder and bill data members are changed from const function because they are declared as mutable.
The keyword mutable is mainly used to allow a particular data member of const object to be modified. When we declare a function as const, the this pointer passed to function becomes const. 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 updatable. Data members declared as mutable can be modified even though they are the part of object declared as const. You cannot use the mutable specifier with names declared as static or const, or reference.
As an exercise predict the output of following two programs.
CPP
// PROGRAM 1
#include <iostream>
using std::cout;
class Test {
public:
int x;
mutable int y;
Test() { x = 4; y = 10; }
};
int main()
{
const Test t1;
t1.y = 20;
cout << t1.y;
return 0;
}
CPP
// PROGRAM 2
#include <iostream>
using std::cout;
class Test {
public:
int x;
mutable int y;
Test() { x = 4; y = 10; }
};
int main()
{
const Test t1;
t1.x = 8;
cout << t1.x;
return 0;
}
Source:
The mutable storage class specifier (C++ only)
This article is contributed Meet Pravasi.
Similar Reads
Const keyword in C++ In this article, the various functions of the const keyword which is found in C++ are discussed. Whenever const keyword is attached with any method(), variable, pointer variable, and with the object of a class it prevents that specific object/method()/variable to modify its data items value.Constant
10 min read
C++ Variables In C++, variable is a name given to a memory location. It is the basic unit of storage in a program. The value stored in a variable can be accessed or changed during program execution.Creating a VariableCreating a variable and giving it a name is called variable definition (sometimes called variable
4 min read
C++ Variables In C++, variable is a name given to a memory location. It is the basic unit of storage in a program. The value stored in a variable can be accessed or changed during program execution.Creating a VariableCreating a variable and giving it a name is called variable definition (sometimes called variable
4 min read
Unary Operators In C++ In C++, unary operators are the type of operators that work on a single value (operand). They perform operations like changing a value's sign, incrementing or decrementing it by one, or obtaining its address.C++ has a total of 9 unary operators:Table of ContentIncrement Operator (++)Decrement Operat
6 min read
map operator= in C++ STL The map::operator= is a built function in C++ STL which assigns contents of a container to a different container, replacing its current content. Syntax: map1_name = map2_name Parameters: The map on the left is the container in which the map on the right is to be assigned by destroying the elements o
2 min read
set operator= in C++ STL The â=â is an operator in C++ STL which copies (or moves) a set to another set and set::operator= is the corresponding operator function. There are three versions of this function: The first version takes reference of an set as an argument and copies it to an set. Syntax: ums1.operator=(set &set
2 min read