mutable:
用于修饰 被const修饰的成员函数要访问的非静态成员变量,被const修饰的成员函数不能修改成员变量的数据,mutable突破const的限制,但仅仅用于改变与类对象状态无关的的成员变量
#include <iostream>
using namespace std;
class text {
public:
text() : num(0) {}
int Print() const;
int getNum() {
return num;
}
private:
mutable int num;
int a;
};
int text::Print() const
{
num++;
/// a++; Error
cout << "Print" << endl;
}
int main()
{
text t;
t.Print();
t.Print();
t.Print();
cout << t.getNum() << endl;
return 0;
}