常函数 常对象

1           常函数 常对象

1.1     常函数  void func() const {} 常函数

1.2     常函数 修饰是this指针  const Type * const this

1.3     常函数 不能修改this指针执行的值

1.4     常对象 在对象前 加入 const修饰 const Person p1

1.5     常对象 不可以调用普通的成员函数

1.6     常对象 可以调用常函数

1.7     用mutable修饰的关键字是在常函数可以修改的

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;

class Person
{
public :
	Person()
	{
		//构造中修改属性
		//this 永远执行本体  
		
		this->m_A = 0;
		this->m_B = 0;
	}

	void showInfo() const //常函数 不允许修改指针指向的值
	{
		//this->m_A = 1000;
		this->m_B = 1000;
		// const Person * const this
		cout << "m_A = " << this->m_A << endl;
		cout << "m_B = " << this->m_B << endl;
	}

	void show2() const
	{
		//m_A = 100;
	}

	int m_A;
	mutable int m_B; //就算是常函数 我还是执意要修改

};

void test01()
{

	Person p1;
	p1.showInfo();

	//常对象 不允许修改属性
	const  Person p2;
	//cout << p2.m_A << endl;
	p2.show2();
	//常对象 不可以调用普通成员函数
	//常对象 可以调用常函数
}

int main(){

	test01();

	system("pause");
	return EXIT_SUCCESS;
}

  

原文地址:https://www.cnblogs.com/Malphite/p/14394168.html