当引用作为函数的返回值时,可以直接将其当作赋值语句的左值使用
如:函数refValue(int& x)可以像 a=10 中的“a”来使用
1 #include <iostream> 2 using namespace std; 3 int oneX = 10;//定义一个变量oneX,并赋值为10 4 int oneY = 20;//定义一个变量oneY,并赋值为20 5 int& refValue(int& x)//定义一个函数refValue,返回值是引用,可以直接作为赋值语句中的左值使用 6 { 7 return x; 8 } 9 int main() 10 { 11 refValue(oneX) = 30;//直接将30赋予给oneX 12 cout << "oneX=" << oneX << endl; 13 refValue(oneY) = 40;//直接将40赋予给oneY 14 cout << "oneY=" << oneY << endl; 15 return 0; 16 }