I have a class with a method (getter) that performs some relatively expensive operation, so I want to cache its result. Calling the method does not change behavior of the object, but it needs to store its result in this
, so it can't be const.
The problem is that I now have another method that is const and I need to invoke my getter. Is there some generally accepted solution to this problem? Should I bypass the constness checking in the getter to make it const, (Coudn't that cause problems with optimizing compiler?) or I have to propagate the non-constness to all methods that use this getter?
Example:
class MyClass
{
public:
Foo &expensiveGetter() /*const?*/
{
if (cachedValue == nullptr) {
cachedValue = computeTheValue();
}
return *cachedValue;
}
void myMethod() /* How to make this const? */
{
auto &foo = expensiveGetter();
// etc.
}
private:
Foo *cachedValue = nullptr;
}
I am looking for something like the RefCell
in Rust.
CodePudding user response:
This is one of the situations the mutable
specifier fits particularly well. When a class member is mutable
, that member can be changed even if the enclosing class is const
.
So, if MyClass::cachedValue
is a mutable Foo*
instead of a Foo*
, you can have const
member functions in MyClass
that make changes to cachedValue
and then all calling code can just act on a const MyClass
as normal.