#include<iostream>
using namespace std;
class Person
{
public:
//this指针的实质是 指针常量 即指针的指向不可以被修改
//Person *const this;
//const Person*const this 表示指针的指向和成员变量的值都不可以改变
//const修饰函数 变为常函数
void func1()const
{
//加上const后值不可以被修改
/*m_A = 100;*/
m_B = 200;
}
void fun2()
{
}
int m_A;
//mutable可变的
mutable int m_B;
};
//常对象
void test01()
{
const Person p;
p.func1();
//常对象不可以(未加 mutable 修饰)修改成员变量
/*p.m_A = 100;*/
p.m_B = 300;
//常对象只能调用常函数
/*p.func2();*/ //fun2(}不是常函数
p.func1();
}
const修饰成员函数
最新推荐文章于 2024-06-18 13:11:16 发布