class xxx{
void showInfo()
const{//常函数不允许修改指针指向的值
//this->m_A = 1000; ==> const Person* const this
this->m_B = 100;
cout << "m_A " << this->m_A << endl;
cout << "m_B " << this->m_B << endl;
}
void show2() const{
//m_A = 100;
cout << "普通方法" << endl;
}
int m_A;
mutable int m_B; //就算是常函数,我还是执意要修改
}
//常对象 不允许修改属性
void test(){
const Person p2;
cout << p2.m_A << endl;
//p2.show2(); 不可以调用普通成员函数,只可以调用常函数
p2.show2();
}
原文:https://www.cnblogs.com/lodger47/p/14693104.html