c++day03 const修饰函数是修饰谁

大家好,我是阿林。今天我和大家分享的内容是c++中的const的修饰的对象的内容。

我想提出一个问题,我定义的void opVar(int a, int b) const这个函数中const修饰的是谁。

其实const 修饰的是既不是形参a也不是形参b 而是修饰的是属性this->a this->b。下面我我们就痛代码去测试一下相关的结论吧。

#include <iostream>

using namespace std;

class Test
{
public:
	Test(int a,int b)
	{
		this->a = a;
		this->b = b;
	}
	void printT()
	{
		cout << "a; " << a << endl;
		cout << " b: " << b << endl;

	}

	//1 const 写在什么位置 没关系
	//2 const 修饰的是谁?
	//const 修饰的是形参a 不是
	//const 修饰的是属性this->a this->b
	void opVar(int a, int b) const // 等价为 void opVar(const Test *this,int a,int  b)
	{
		a = 100; //能够运行代表const不是修饰啊;

		//this->a = 100; //编译器报错证明是不可修改的,所以修饰this->a this->b


		cout << "a:" << a << endl;
		cout << "b: " << this->b << endl;
	}

private:
	int a;
	int b;
};



int main()
{
	system("pause");
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值