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:
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.
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
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.
mutable
for members that should logically be modifiable in const
methods, like logging or caching.mutable
to avoid confusion for other developers.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.
Answers to questions are automatically generated and may not have been reviewed.
An introduction to the mutable
keyword, which gives us more flexibility when working with const
objects.