C++

C++ - [Concurrency 10] Mutable Variables

Posted by Rico's Nerd Cluster on June 1, 2023

Mutable Variable

In this example, we are able to change a mutable variable in a const function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>

class Foo {
public:
    Foo(std::string name) : name_(std::move(name)) {}

    // This function is logically "const" (it doesn't change the observable state
    // of the object), but we still want to keep a call counter.
    void do_something() const {
        // allowed because cnt_frame_ is mutable
        ++cnt_frame_;

        std::cout << "Foo(" << name_ << ") do_something called "
                  << cnt_frame_ << " times\n";
    }

    std::size_t call_count() const {
        return cnt_frame_;
    }

private:
    std::string name_;
    mutable std::size_t cnt_frame_ = 0;  // can be modified in const functions
};