内容
- 常函数 常对象
7.1常函数 void func() const {}常函数
7.2常函数 修饰是this指针const Type * const this
7.3常函数 不能修改this指针执行的值
7.4常对象 在对象前 加入 const修饰 const Person p1
7.5常对象 不可以调用普通的成员函数
7.6常对象 可以调用常函数
7.7用mutable修饰的关键字是在常函数可以修改的
代码
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Person
{
public :
Person()
{
this->m_A = 0;
this->m_B = 0;
}
void showInfo() const
{
this->m_B = 1000;
cout << "m_A = " << this->m_A << endl;
cout << "m_B = " << this->m_B << endl;
}
void show2() const
{
}
int m_A;
mutable int m_B;
};
void test01()
{
Person p1;
p1.showInfo();
const Person p2;
p2.show2();
}
int main(){
test01();
system("pause");
return EXIT_SUCCESS;
}