常函数:在成员函数后加const
常对象:在类型前面加const
mutable关键字:可以使成员变量被成员函数改变
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class person
{
public:
//常函数
//1.在函数声明后面加const关键字
//2.作用:防止this指向的变量被修改
//3.如果有得变量必须要修改,可以在变量前面加上mutable关键字
void func1() const
{
//this->m_a = 10;
this->m_b = 10;
}
int m_a;
mutable int m_b;//加上mutable关键字,就可以在常函数中更改
};
int main()
{
//常对象
//1.在类型前面加const关键字
//2.作用:使此对象只能访问常成员函数,不能访问其他成员函数。
const person p;
p.func1();
system("pause");
return EXIT_SUCCESS;
}