网站推广关键词排名优化,ppt免费下载的网站,福泉网站制作,网站转html5this 指针的本质: Person * const this; 常函数 用 const 修饰成员函数时#xff0c;const 修饰 this 指针指向的内存区域#xff0c;成员函数体内不可以修改本类中的任何普通成员变量。 void show() const
{
}//const Person const this;//限制了 this 指针当成员变量类型符… this 指针的本质: Person * const this; 常函数 用 const 修饰成员函数时const 修饰 this 指针指向的内存区域成员函数体内不可以修改本类中的任何普通成员变量。 void show() const
{
}//const Person const this;//限制了 this 指针当成员变量类型符前用 mutable 修饰时例外。 mutable int m_A;常对象 常对象只能调用 const 的成员函数。 常对象可访问 const 或非 const 成员属性不能修改除非成员用 mutable 修饰。 案列
//const修饰成员函数
class Person{
public:Person(){this-mAge 0;this-mID 0;}//在函数括号后面加上const,修饰成员变量不可修改,除了mutable变量void sonmeOperate() const{//this-mAge 200; //mAg e不可修改this-mID 10;}void ShowPerson(){cout ID: mID mAge: mAge endl;}
private:int mAge;mutable int mID;
};int main(){Person person;person.sonmeOperate();person.ShowPerson();system(pause);return EXIT_SUCCESS;
}const修饰类对象
class Person{
public:Person(){this-mAge 0;this-mID 0;}void ChangePerson() const{mAge 100;mID 100;}void ShowPerson(){this-mAge 1000;cout ID: this-mID Age: this-mAge endl;}public:int mAge;mutable int mID;
};void test(){ const Person person;//1. 可访问数据成员cout Age: person.mAge endl;//person.mAge 300; //不可修改person.mID 1001; //但是可以修改mutable修饰的成员变量//2. 只能访问const修饰的函数//person.ShowPerson();person.ChangePerson();
} 不能直接修改, 但是可以通过指针间接修改。