Introduction to C++ Mutable
The following article provides an outline for C++ Mutable. In C++ there is a special facility where you can change value of data members at runtime even if the object is of constant type. The mutable keyword helps us only on non-static and non const data members of the class. It helps in assigning value to this constant value even though the member is constant. There are times when it is required that only two members take part like a variable and a constant 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 making changes in the flag by incrementing the value of m_Count variable. The mutable function can be changed as per necessity. Hence the variable m_Count is declared mutable and it is getting increased in the constant function.
It will store the value of m_Count variable initially and once it encounters the value as mutable it will change the value and increment the value of this variable whenever it is called. It gets changed dynamically whenever the mutable variable is called. This value is returned and sent it as a Boolean 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 are the variables which can be changed at runtime. Here these two variables are ordertobeplaced and billamt. We have declared these two functions as mutable in our class Customer. The public constructor is having 4 parameters defined. We have copied the name and order which is placed in two variables. 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. The point to be noted here is that the Customer object c1 here is a constant object.
- At first it will display the order of Veg Burger having price as 100. After this we call the functions which use the mutable variables. Here we change the values to Veg Lasagne and price to 150. When the display function is called both the orders with their respective prices will be displayed.
Output:
Example #2
Example where 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 non mutable function. This function is a constant function. But as the mutnum is mutable we will be able to 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 as 10 and 20 for mutable and non mutable variables respectively. The commented line will throw an error if we uncomment it. This is because that variable is constant and also we have not defined it mutable explicitly.
- We will check that output in a while. Let us check the output when that statement is commented. The mutable number will have its first value 10 and then it will be changed 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. We are trying to display the variable which was not declared as mutable and is constant. Make 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 states that num is not a mutable value and it is only read-only object as it is declared a constant.
Conclusion
The mutable keyword in C++ helps in changing 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 mutable class works along with programming examples. You may also have a look at the following articles to learn more –
4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses