c++问题:为什莫要引入mutable来修饰数据成员?
是为了让你在const函数里头修改它修饰的变量。
既然是const为什么还要修改变量?
有时候const函数不修改关键的值,只是维护一些统计信息之类,就可以使用.
For example, the following code will compile without error because m_accessCount has been declared to be mutable, and therefore can be modified by GetFlag even though GetFlag is a const member function.
class X
{
public:
bool GetFlag() const
{
m_accessCount++;
return m_flag;
}
private:
bool m_flag;
mutable int m_accessCount;
};
参考资料:ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_vclang/html/ebe89746-3d36-43a8-8d69-f426af23f551.htm