Skip to main content

Mutable Data Members in C++

|

Mutable data members in C++ are those whose values can be changed at runtime, even if the object they belong to is declared as constant. This is the opposite of constant members, which cannot be modified once they are set.


In certain situations, logic may require some data members to be variable while others remain constant. The concept of mutability is especially helpful in managing such classes effectively.


Example

Here’s a practical example to illustrate the concept:

#include <iostream>

using namespace std;


class Test {

   public:

      int a;

      mutable int b;


      Test(int x=0, int y=0) {

         a = x;

         b = y;

      }


      void seta(int x=0) {

         a = x;

      }


      void setb(int y=0) {

         b = y;

      }


      void disp() const {

         cout << endl << "a: " << a << " b: " << b << endl;

      }

};


int main() {

   const Test t(10, 20);

   cout << t.a << " " << t.b << "\n";

   // t.a = 30; // Error: 'a' cannot be changed because the object is constant.

   t.b = 100; // 'b' can still be changed because it is mutable.

   cout << t.a << " " << t.b << "\n";

   return 0;

}


In this example, the class `Test` contains two data members: `a` and `mutable int b`. 

The `mutable` keyword allows `b` to be modified even when the object `t` is declared as `const`. 

When attempting to modify `a`, an error occurs because `a` is not mutable and the object is constant. 

However, `b` can still be changed due to its mutable nature. 

This demonstrates how mutable data members can be used to manage class data effectively while preserving the const-correctness of the object's interface.


Comments

Popular posts from this blog

Understanding push_back and emplace_back in C++

| Understanding push_back and emplace_back in C++ C++ provides several mechanisms to add elements to its containers, and two often used are push_back and emplace_back . Understanding the difference between these methods can help you write more efficient and expressive code. Let's delve into these concepts with examples to illustrate their usage and benefits.

Reasons for a C++ Program Crash

C++ programs may crash unexpectedly for various reasons. Here are some typical causes of such crashes: Segmentation Fault A segmentation fault is a major cause of program crashes. It occurs when: Attempting to access a memory location that doesn’t exist. Trying to write to a read-only memory location. Accessing protected memory locations, such as kernel memory. Example: int main() {     char *text;     // Stored in the read-only part of the data segment     text = "ABC";     // Problem: trying to modify read-only memory     *(text + 1) = 'n';     return 0; }   Stack Overflow Stack overflow happens due to non-terminating recursion, which exhausts the stack memory. Example: #include <stdio.h> void functionRecursive(int num)  {     if (num =...

Local Classes in C++: A Deep Dive

Understanding Local Classes in C++: A Deep Dive A class declared within a function is known as a local class in C++. These classes are specific to the function in which they are declared and offer several unique characteristics and constraints. What is a Local Class? A local class is a class defined within a function. For instance, in the following example, the Test  class is local to the fun()  function: #include<iostream>  using namespace std;  void fun()    {      class Test  // local to fun      {          // members of the Test class     };  }  int main()  {      return 0;  } Interesting Facts About Local Classes Scope Limitation A local class type name can only be used within the enclosing function. For example, in the following program, the LocalClass is valid within myFunction() , but not in main() . #include <iostream> void myFunct...