C++

Asked • 08/22/19

Does the 'mutable' keyword have any purpose other than allowing the variable to be modified by a const function?

A while ago I came across some code that marked a member variable of a class with the `mutable` keyword. As far as I can see it simply allows you to modify a variable in a `const` method: class Foo { private: mutable bool done_; public: void doSomething() const { ...; done_ = true; } }; Is this the only use of this keyword or is there more to it than meets the eye? I have since used this technique in a class, marking a `boost::mutex` as mutable allowing `const` functions to lock it for thread-safety reasons, but, to be honest, it feels like a bit of a hack.

1 Expert Answer

By:

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.