
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ mutable keyword?
The mutable keyword in C++ is a storage class specifier. It enables non-static, non-const, and non-reference data members (i.e., mutable data member) of a class to be changed even when the object containing them is declared constant.
Mutable data members are those members whose values can be changed in runtime even if the object is of constant type. It is just the opposite of a constant.
What is Need of Mutable?
Sometimes, it is necessary to modify one or more data members of a class/struct through the constant function, even if you do not want the function to update another member of the class. So, we use the mutable keyword to perform the task easily.
For Example: Suppose we go to a hotel to place an order. Once the order is placed, our details and table number are stored in a constant. However, the order itself needs to be changed; you might want to order something or replace some items. Even if the customer object is declared as constant, you will still want to change the order and bill.
Example of Mutable Keyword
In the following example, we demonstrate the working of the mutable keyword in C++:
#include <iostream> using namespace std; class Test { public: int a; mutable int b; // Explicit constructor to avoid implicit conversions. explicit Test(int x = 0, int y = 0) : a(x), b(y) {} // Setter for 'a'. Since it modifies the object, it can't be used with const objects. void seta(int x = 0) { a = x; } // Setter for 'b'. It can modify 'b' even in const objects because 'b' is mutable. // Marked as const to allow it to be called on const objects. void setb(int y = 0) const { b = y; } // Display function for the object's state. Marked as const since it doesn't modify the object. void disp() const { cout << endl << "a: " << a << " b: " << b << endl; } }; int main() { const Test t(10, 20); // Const object 't', 'a' cannot be modified, but 'b' can. cout << "Initial values:" << endl; cout << "a: " << t.a << " b: " << t.b << "\n"; // t.a = 30; // Uncommenting this will result in an error as 'a' is part of a const object. t.setb(100); // 'b' can be changed because it is mutable, and setb is now const. cout << "After modifying b:" << endl; cout << "a: " << t.a << " b: " << t.b << "\n"; return 0; }
Following is the output:
Initial values: a: 10 b: 20 After modifying b: a: 10 b: 100
Change Order Using Mutable Keyword
In the following example, we update the order and bill of the above-explained example scenario and demonstrate the use of mutable keywords:
#include <iostream> #include <string> using namespace std; class Customer { string name; int table_no; mutable string order; mutable int bill; public: Customer(string n, int t, string o, int b): name(n), table_no(t), order(o), bill(b) {} void update_order(string new_order) const { // it can be change 'order' is mutable order = new_order; } void update_bill(int new_bill) const { // It can also be change because 'bill' is mutable bill = new_bill; } void display() const { cout << "Customer: " << name << "\nTable: " << table_no << "\nOrder: " << order << "\nBill: /-" << bill << endl; } }; int main() { const Customer c1("Aman", 5, "Pasta", 300); c1.display(); c1.update_order("Pasta + Ice Cream"); c1.update_bill(400); cout << "\nAfter updates:\n"; c1.display(); return 0; }
Following is the output:
Customer: Aman Table: 5 Order: Pasta Bill: /-300 After updates: Customer: Aman Table: 5 Order: Pasta + Ice Cream Bill: /-400