【面试题5】

考点:参数引用的常见错误

#include <iostream>
using namespace std;
class Test
{
public:
	void f(const int & arg);
private:
	int value;
};
void Test ::f(const int & arg) 
{
	arg = 10;                   // arg 是 const int 型的引用 是一个常量引用,所以 arg 在函数内不能被修改
	cout << arg << endl;
	value = 20;

}
int main()
{
	int a = 7;
	const int  b = 10;
	int &c = b;                // b 是一个 常量,c 是一个 int 型的引用不能将b赋给c

	const int &d = a;          

	a++;
	d++;                      // d 是一个 const int 型的引用 即 常量引用 不能进行赋值操作

	Test test;
	test.f(a);

	cout << a << endl;

	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值