Updated June 17, 2023
Introduction to C++ Mutable
The following article provides an outline for C++ Mutable. In C++, there is a special facility where you can change the value of data members at runtime, even if the object is of constant type. The mutable keyword helps us only on non-static and nonconst data class members. It helps assign value to this continuous value even though the member is regular. In this situation, mutability is very helpful.
Syntax:
mutable member-variable-declaration;
We can declare any object or variable in a program by using the mutable keyword. We can modify the class member whenever the object is declared as constant.
How Mutable Class works in C++?
Given below shows how the mutable function work :
Code:
class exp
{
public:
bool FlagFunction() const
{
m_Count++;
return m_flag;
}
private:
bool m_flag;
mutable int m_Count;
};
int main()
{
}
The mutable function enables us to modify even a constant variable. Here we have a function where we have a class. The mutable function works on constant functions and variables. The flag function here is a constant function. We are changing the flag by incrementing the value of m_Count variable.
Examples of C++ Mutable
Given below are the examples mentioned:
Example #1
Changing the mutable variables.
Code:
#include <iostream>
#include <string.h>
using std::cout;
using std::endl;
class Customer
{
char name[25];
mutable char ordertobeplaced[50];
int tableNumber;
mutable int billamt;
public:
Customer(char* s, char* m, int a, int p)
{
strcpy(name, s);
strcpy(ordertobeplaced, m);
tableNumber = a;
billamt = p;
}
void PlacedOrder(char* p) const
{
strcpy(ordertobeplaced, p);
}
void BillChanged(int s) const
{
billamt = s;
}
void display() const
{
cout << "The name of Customer is: " << name << endl;
cout << "Order placed by Customer is : " << ordertobeplaced << endl;
cout << "The table number od the Customer is: " << tableNumber << endl;
cout << "The total bill amount is: " << billamt << endl;
}
};
int main()
{
const Customer c1("Snehal Sawant", "Veg Burger", 3, 100);
c1.display();
c1.PlacedOrder("Veg Lasagne");
c1.BillChanged(150);
c1.display();
return 0;
}
Explanation:
- The above code is using two mutable variables. These two variables, ordertobeplaced and billamt, can be modified at runtime. We have declared these two functions as mutable in our class Customer. We have copied the name and order from the public constructor and assigned them to two variables. The public constructor itself is defined with four parameters. We have also taken variables like tableNumber and billamt, which are not mutable.
- The PlacedOrder function is copying the order which is currently placed. The BillChanged function is using the billamt, which is currently present in the mutable variables. These two functions operate on these two mutable variables. The display function displays these details as the program runs and has different changes in it.
- At first, it will display the order of Veg Burger having a price of 100. After this, we call the functions which use the mutable variables. Here we change the values to Veg Lasagne and the price to 150. When the display function is called, both orders with their respective prices will be displayed.
Output:
Example #2
For example, when we try to change an immutable variable.
Code:
#include <iostream>
using namespace std;
class TestMutable {
public:
int num;
mutable int mutnum;
TestMutable(int x=0, int y=0) {
num=x;
mutnum=y;
}
void setnum(int x=0) {
num = x;
}
void setb(int y=0) {
mutnum = y;
}
void disp() const{
cout<<endl<<"NUmber: "<<num<<" Mutable Number: "<<mutnum<<endl;
}
};
int main() {
const TestMutable t(10,20);
cout<<t.num<<" "<<t.mutnum<<"\n";
// t.num=30; //Uncommenting this will throw an error as num is a constant and not mutable
t.mutnum=100; //mutnum can be changed any number of times as it is mutable
cout<<t.num<<" "<<t.mutnum<<"\n";
t.disp();
return 0;
}
Explanation:
- The above example has taken a TestMutable function where we are using one mutable and one nonmutable function. This function is a constant function. But as the mutnum is mutable, we can change this variable’s value. We are setting the number to these two variables and then using it in the display function to display its value.
- We have sent the numbers 10 and 20 for mutable and nonmutable variables, respectively. The commented line will throw an error if we uncomment it. This is because that variable is constant, and we have not defined it as mutable explicitly.
- We will check that output in a while. The mutable variable “number” will initially have a value of 10 and then change it to 100. While 20 will remain unchanged. It will be as below.
Output:
Now let us try to display by uncommenting the code as below. Make the below changes in the main.
Code:
int main() {
const TestMutable t(10,20);
cout<<t.num<<" "<<t.mutnum<<"\n";
t.num=30; //Uncommenting this will throw an error as num is a constant and not mutable
t.mutnum=100; //mutnum can be changed any number of times as it is mutable
cout<<t.num<<" "<<t.mutnum<<"\n";
t.disp();
return 0;
}
Output:
The error message indicates that the variable “num” is not mutable and has been declared as a constant, making it a read-only object.
Conclusion
The mutable keyword in C++ helps in changing the values of constant variables. The value can be changed at runtime. You can explicitly define a mutable variable and use it throughout the program.
Recommended Articles
This is a guide to C++ Mutable. Here we discuss the introduction to C++ Mutable, how a mutable class works, and programming examples. You may also have a look at the following articles to learn more –