const常对象:
不允许修改成员变量的值
常对象只能调用常函数(因为其他普通函数很可能修改成员变量)
#include<iostream>
#include<cstdio>
using namespace std;
class T{
public:
int a = 10;
mutable int b = 11;
static int c;
public:
// 相当于 const 修饰了 this 指针
void func() const{
b = 13;
// a = 13; 不允许修改 this 所指向的内容
}
};
int T::c = 12;
int main()
{
const T t = T();
cout<<t.a<<endl;
t.func();
// t.a = 15;
cout<<t.c<<endl;
t.c = 16;
cout<<t.c<<endl;
return 0;
}