C++ 左值表达式

左值表达式可以是变量,指针和引用,所以函数的返回值是这些类型,也能用在赋值号左边。

#include <iostream>
using namespace std;

int x = 10;
int y = 20;

int & refValue(int & x){
	return x;
}

int main(){
	cout << "leftValue before \tx: " << x << '\n';
	refValue(x) = 30;
	cout << "leftValue after \tx: " << x << '\n';
	cout << "-------------------------------\n";
	cout << "leftValue before \ty: " << y << '\n';
	refValue(y) = 40;
	cout << "leftValue after \ty: " << y << '\n';
	return 0;
}

image

posted @ 2023-02-26 12:42  快乐在角落里  阅读(103)  评论(0)    收藏  举报