Mutable keyword in C++

Hi everybody! today we are going to learn about Mutable keyword in C++. As we know that in C++, a keyword is a reserved word which has a specified meaning. There are several keywords in C++ like const, static, explicit, etc.

Mutable Keyword

One such keyword is mutable, it’s working is the exact opposite of const keyword. In const keyword the value of a variable cannot be changed during the entire program, but what if someone feels a need to change it.

Mutable keyword comes in handy when in a const declared object, you want to update a few constant data members without updating other data members. For example when you buy a property or land and sell it after some time. The dimension of the property remains the same, its location remains the same just the details of the owner needs to be changed. In this case, you can use the mutable keyword for changing the Owner’s Name.

Example of Mutable Keyword in C++

Let understand how to use it with the help an example.

#include <iostream.h>
#include <string.h>

using namespace std;
class property{
    mutable char OwnerName[20];
    float area;
    char location[50];
    property(char *o,float a,char *l)
        strcpy(OwnerName ,o);

    void changeOwner(char *o) const{
    void changelocation(char *l) {
    void display()const{
    cout <<endl<<"Owner Name= "<<OwnerName<<endl<<"Area= "<<area<<endl<<"Location= "<<location;
int main()
   const property p("Biharilal",89.6,"Faridabad");


//   p.changelocation("Gurgaon");
 // p.display();
    return 0;


Owner Name= Biharilal
Area= 89.6
Location= Faridabad
Owner Name= Ramchandra
Area= 89.6
Location= Faridabad

Here if we uncomment the lineĀ  p.changelocation(“Gurgaon”); in our code, this will show an error. any attempt to change a constant data member will display an error because the object p is declared const hence none of its data members can change. But still, if you need to change it’s data member, by using the mutable keyword you can do so.

Hope you found the article helpful!

Leave a Reply

Your email address will not be published. Required fields are marked *