The Mutable Storage Class in C++



The mutable storage class in C++ is a property that gives you access to modify the non-static data members (not static data members) of a class, even when the object is declared as constant. This is mainly useful for scenarios where the data needs modification without affecting the logical state of the object, like caching, lazy initialization, and logging.

Syntax

class class_name {
    mutable data_type member_name;
};

Here is the following syntax for the mutable storage class, which is declared using the mutable keyword and applied to only non-static data members of a class.

Example

#include <iostream>
using namespace std;

class Student {
    mutable int attendance;  // this variable can be changed even in const objects

public:
    Student() : attendance(0) {}  // constructor initializes attendance to 0
    // const function (can't change/modify normal data members)
    void mark() const {  
        attendance++;  // but able to modify the mutable member attendance
        cout << "Current total attendance: " << attendance << endl;
    }
};

int main() {
    // 's' is a constant object which usually can't modify its members
    const Student s;  

    s.mark();  // still allowed! because 'attendance' is mutable
    s.mark();  // increases attendance again

    return 0;
}

Output

Current total attendance: 1
Current total attendance: 2

In the following example, we can see how a constant object and function are modified and accessed even though they were declared as constant, because of the mutable keyword.

Real-life Use Case

This can be useful to track and modify only certain internal data as required when the object is considered logically constant.

Here we will look at an example of accessing a Read-Only File. In this, we want to track the number of times the file has been accessed without modifying the object itself, which makes it a constant object for the user, but we will be able to update the internal accessCount for logging purposes.

Example

#include <iostream>
using namespace std;

class ReadOnlyFile {
    string fileName;
    mutable int count;  // Mutable to allow modification even in const object

public:
    ReadOnlyFile(string name) : fileName(name), count(0) {}

    void accessFile() const {
        count++;  // Increment access count, even though the object is const
        cout << "File '" << fileName << "' accessed " << count << " times." << endl;
    }

    void showFileDetails() const {
        cout << "File name: " << fileName << endl;
    }
};

int main() {
    const ReadOnlyFile file("document.txt");  // Constant object (user can't modify fileName)

    file.accessFile();  // Log access (count can change because it's mutable)
    file.accessFile();
    file.showFileDetails();

    return 0;
}

Output

File 'document.txt' accessed 1 times.
File 'document.txt' accessed 2 times.
File name: document.txt

In the following example code, we can see how the mutable keyword is used to track the number of times the file is accessed, without changing the actual content of the object. This makes the object unchanged to the user (ensuring important data remains protected), but allows internal updates like access counting.

Updated on: 2025-05-12T19:38:58+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements