//#include<iostream>
//using namespace std;
//
//
成员函数后加const后我们称为这个函数为常函数
常函数内部不可以修改成员属性
成员属性声明时加关键字mutable后在常函数中依然可以修改
//
//class Person
//{
//public:
// //this 指针的本质是指针常量 指针指向是不可以被修改的
// //Person * const this;const
// void showPerson() const//不想被修改就在后面加一个const
// {
// //m_A = 100;
// //this = NULL; this指针不可以修改指针的指向的
// }
//
// int m_A;
// mutable int m_B;
//
//};
//
//void test01()
//{
// Person p;
// p.showPerson();
//}
//
常对象
声明函数前加const称该对象为常对象
常对象只能调用常函数
//void test02()
//{
// const Person p;//变为常对象
// p.m_B = 1;//因为m_B前面有mutable所以可以更改
//
// //常对象只能调用常函数
// p.showPerson();
// //p.func(); 常对象你不可以调用普通成员函数,因为普通成员函数可以修改属性
//}
//
//int main()
//{
// test01();
//
// return 0;
//}
const修饰成员函数
最新推荐文章于 2024-09-16 17:46:36 发布