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.

constexpr in C++

|  Let’s dive into the depths of constexpr in C++! constexpr is short for "constant expression." It was introduced in C++11 and further enhanced in C++14 and C++20. The primary purpose of constexpr is to allow the evaluation of expressions at compile-time, enabling several powerful optimizations. Here’s a detailed breakdown: Purpose of constexpr The idea behind constexpr  is to inform the compiler that the value of a variable or the result of a function can be determined at compile-time. It will be if the expression can be evaluated at compile-time, resulting in performance benefits. It’s beneficial for: - Compile-time constants: Values that don’t change at runtime. - Optimizations: Allowing the compiler to optimize code more effectively. - Template metaprogramming: Enhancing the power of templates. Usage in Variables A constexpr  variable must be initialized with a constant expression.  Here’s an example: constexpr int length = 10; constexpr int width = 5; conste...

When do we use Initializer List in C++?

An initializer list is used to initialize the data members of a class. This list of members to be initialized is specified in the constructor as a comma-separated list, followed by a colon. Here is an example that demonstrates the use of an initializer list to initialize the variables x and y in the Point class. #include<iostream>  using namespace std;     class Point {  private:      int x;      int y;  public:      Point(int i = 0, int j = 0):x(i), y(j) {}       /*  The above use of the Initializer list is optional as the           constructor can also be written as:          Point(int i = 0, int j = 0) {              x = i;              y = j;          }      */              ...