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.
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…