C++ note------const修饰对象

tech2022-07-05  184

常函数:

1.成员函数后加const

2.常函数内不可以修改成员属性

3.成员属性声明加关键字mutable后,在常函数中便可以修改

常对象: 1.声明对象前加const 2.常对象只能调用常函数

#include <iostream> using namespace std; class Person { public: void showPerson()const { //this->m_A = 100;//错误语句 m_B = 100; } void fun() { } int m_A; mutable int m_B; }; void test() { const Person p; //p.fun();//错误语句 p.showPerson(); } int main() { test(); system("pause"); }
最新回复(0)