mutable c++

  The keyword mutable is used to allow a particular data member of const object to be modified. This is particularly useful if most of the members should be constant but a few need to be updateable. 

Test:

#include "stdafx.h"

class TestMutable
{
public:
    void love() const
    {
        id_ = 11;
        value_ = 12;
    }

private:
    int id_;
     mutable int value_;
};

int _tmain(int argc, _TCHAR* argv[])
{
    TestMutable test;
    test.love();

    return 0;
}

 

id_ = 11; 这句编译时将会报错。 testmultable.cpp(11) : error C2166: l-value specifies const object
value_ = 12; 可以通过, mutable 关键字指定value_是易变的。
 

posted on 2014-04-24 15:36  偏爱省略号  阅读(195)  评论(0编辑  收藏  举报

导航