Mutable Class Members

When Not to Use mutable

Can you provide an example where using mutable is not recommended?

Abstract art representing computer programming

While the mutable keyword can be useful, there are scenarios where its use is not recommended. One primary reason to avoid mutable is if it breaks the const-correctness concept of your class, making your code harder to understand and maintain.

There are two examples where using mutable would be inappropriate

1. Breaking Logical Constness

If modifying a mutable member significantly alters the state of an object, it breaks the promise that a const method should not change the object's logical state. This can lead to unexpected behaviors and bugs.

Consider a class that represents a bank account:

#include <iostream>

class BankAccount {
public:
  BankAccount(double balance) : balance{balance} {}

  double GetBalance() const {
    // This function should not change any logical state
    return balance;
  }

  void Withdraw(double amount) const {
    if (balance >= amount) {
      // This modifies the balance in a const method
      balance -= amount; 
    }
  }

private:
  mutable double balance; 
};

int main() {
  const BankAccount account{100.0};
  account.Withdraw(50.0); 
  std::cout << "Balance: " << account.GetBalance();
}
Balance: 50

In this example, using mutable on the balance variable is misleading because it allows modification of the account balance in a const method, which breaks the expectation that const methods should not change the object's logical state.

2. Confusing Design

Overusing mutable can make the code confusing and difficult to follow, especially for other developers who might not expect certain variables to be modified in const methods. This can lead to maintenance challenges and potential bugs.

In summary, use mutable sparingly and only when it makes sense to modify certain internal variables that do not affect the logical state of the object. Avoid using it when it compromises the integrity of the object's design and the principle of const-correctness.

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:

  • 125 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