028_this指针和成员函数中加入const后修饰的是谁

//this指针和const
#if 1

class A
{
public:
	//普通成员函数
	A(int a, int b)// ====》A( A *pThis, int a, int b);
	{
		this->a = a;
		this->b = b;
	}

	void printA()//==>void printA(A * this)
	{
		cout << "a = " << a << endl;
		cout << "b = " << this->b << endl;
	}

	void opVar1(int a, int b)//==> void opVar1(A * const pThis, int a, int b)
	{
		a = 100;
		//若类成员函数的形参 和 类的属性,名字相同,通过this指针来解决。
		this->a = a;
	}

	/*
		void const opVar2(int a, int b);
		void opVar2(int a, int b) const;

	1、const写在什么位置 没有关系
	2、const修饰的是this指针所指向的内存空间,修饰的是this指针.
	3、指针变量与指针所指向的内存空间 是两个不同的概念
	*/

	//类的成员函数可通过const修饰,请问const修饰的是谁?

	//void const opVar2(int a, int b) 
	void opVar2(int a, int b) const //==>void opVar1(const A * const pThis,int a, int b)
	{
		
		a = 100;
		//this->a = 80;//err
	}
protected:
private:
	int a;
	int b;
};
void test()
{
	A a1(1, 2);
	a1.printA();//==> printA(&a1);

}

#endif

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值