考点:参数引用的常见错误
#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;
}