//常函数class Person {public:  Person() {    m_A = 0;    m_B = 0;  }
  //this指针的本质是一个指针常量,指针的指向不可修改  //如果想让指针指向的值也不可以修改,需要声明常函数  void ShowPerson() const {    //常函数本质:const Type* const pointer;常函数的const本身修饰的是this指针,导致指针指向的值和地址都不可改。            //this = NULL; //不能修改指针的指向 Person* const this;    //this->mA = 100; //mA = 100在本函数中的本质是this->mA = 100其中this指针是指针常量,this指针指向的对象的数据是可以修改的,但是加上const声明为常函数后其值也不可以修改了。
    //const修饰成员函数,表示指针指向的内存空间的数据不能修改,除了mutable修饰的变量    this->m_B = 100;  }
  void MyFunc() const {    //mA = 10000;  }
public:  int m_A;  mutable int m_B; //可修改 可变的};
//const修饰对象  常对象void test01() {
  const Person person; //常量对象    cout << person.m_A << endl;  //person.mA = 100; //常对象不能修改成员变量的值,但是可以访问  person.m_B = 100; //但是常对象可以修改mutable修饰成员变量
  //常对象访问成员函数  person.MyFunc(); //常对象不能调用普通成员函数,因为普通成员函数可以修改属性。
}
int main() {
  test01();
  system("pause");
  return 0;}
评论