30 c++
你什么时候使用C++ mutable
关键字?为什么?我认为我不必使用该关键字.我知道它用于诸如缓存(或者可能是记忆)之类的东西,但是你需要在什么类和条件下使用它呢?
Joh*_*mew 46
有时,我使用它来将互斥或其他线程同步原语标记为可变,以便通常标记的访问器/查询方法const
仍然可以锁定互斥锁.
当您需要检测代码以进行调试或测试时,它有时也很有用,因为检测通常需要从内部查询方法中修改辅助数据.
小智 14
在内部成员计算的对象缓存结果的情况下,我使用了mutable:
class Transformation
{
private:
vec3 translation;
vec3 scale;
vec4 rotation;
mutable mat4 transformation;
mutable bool changed;
public:
Node()
{
[...]
changed = false;
}
void set_translation(vec3 _translation)
{
translation = _translation;
changed = true;
}
void set_scale(...) ...
mat4 get_transformation() const
{
if(changed)
{
// transformation and changed need to be mutable here
transformation = f(translation, scale, rotation); // This take a long time...
changed = false;
}
return transformation;
}
};
void apply_tranformation(const Transformation* transfo)
{
apply(transfo->get_transformation());
}
Run Code Online (Sandbox Code Playgroud)
Mar*_*wis 10
Google代码搜索显示了许多用途.例如,在XTR加密的实现中,使用可变成员,以便方法可以返回对结果的引用(防止制作副本).
再举一个例子,Webkit使用它来懒惰地初始化成员数据(m_lineHeight).
在模拟对象中捕获成员变量中 const 函数的参数值。
class Source
{
public:
virtual ~Source() {}
virtual std::string read(int count) const=0;
};
class SourceMock : public Source
{
public:
mutable std::vector<int> arguments;
std::string read(int count) const {
arguments.push_back(count);
return "...";
}
};
//TEST....
SourceMock mock;
//...
VERIFY(mock.arguments.size()==2);
VERIFY(mock.arguments[0]==3);
//...
Run Code Online (Sandbox Code Playgroud)