Mutable Class Members

Mutable in Inheritance

How does the mutable keyword work in the context of inheritance and polymorphism?

Abstract art representing computer programming

The mutable keyword can be used effectively in the context of inheritance and polymorphism, but it requires careful consideration. Here's how it works and some important points to keep in mind:

Inherited Mutable Members

If a base class has a mutable member, derived classes inherit this member with the same mutable properties. This means that the mutable member can be modified within const member functions of the derived class as well.

#include <iostream>

class Base {
 public:
  virtual void Print() const {
    ++callCount;  
    std::cout << "Base call count: "
      << callCount << '\n';
  }

 protected:
  mutable int callCount{0};  
};

class Derived : public Base {
 public:
  void Print() const override {
    ++callCount;  
    std::cout << "Derived call count: "
      << callCount << '\n';
  }
};

int main() {
  const Derived obj;
  obj.Print();  
  obj.Print();  
}
Derived call count: 1
Derived call count: 2

In this example, both the base class and derived class can modify the callCount member even in const member functions.

Polymorphic Behavior

When using polymorphism, mutable members behave consistently regardless of whether you access them through a base class or a derived class pointer/reference.

This allows you to use mutable for shared states like counters or logs across a class hierarchy.

#include <iostream>

class Base {
 public:
  virtual void Print() const {
    ++callCount;
    std::cout << "Base call count: "
      << callCount << '\n';
  }

 protected:
  mutable int callCount{0};
};

class Derived : public Base {
 public:
  void Print() const override {
    ++callCount;
    std::cout << "Derived call count: "
      << callCount << '\n';
  }
};

void PrintDetails(const Base& b) {
  b.Print();  
}

int main() {
  Derived obj;
  PrintDetails(obj);  
  PrintDetails(obj);  
}
Derived call count: 1
Derived call count: 2

Design Considerations

While mutable is useful in inheritance and polymorphism, overusing it can lead to maintenance issues. Ensure that mutable members are truly meant to be modified in const contexts and do not affect the logical state of the objects.

Best Practices

  • Use Sparingly: Only use mutable for members that should logically be modifiable in const methods, like logging or caching.
  • Documentation: Clearly document why a member is mutable to avoid confusion for other developers.
  • Avoid Logical State Changes: Ensure that modifying mutable members does not change the observable state of the object from an external perspective.

In conclusion, the mutable keyword works seamlessly with inheritance and polymorphism, allowing you to maintain shared mutable states across a class hierarchy.

However, careful design and documentation are crucial to prevent misuse and maintain the integrity of your class design.

This Question is from the Lesson:

Mutable Class Members

An introduction to the mutable keyword, which gives us more flexibility when working with const objects.

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

Mutable Class Members

An introduction to the mutable keyword, which gives us more flexibility when working with const objects.

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 124 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved