Lvalues and Rvalues
左值和右值
We'll have more to say about expressions in Chapter 5, but for now it is useful to know that there are two kinds of expressions in C++:
我们在第五章再详细探讨表达式,现在先介绍 C++ 的两种表达式:
lvalue (pronounced "ell-value"): An expression that is an lvalue may appear as either the left-hand or right-hand side of an assignment.
左值(发音为 ell-value):左值可以出现在赋值语句的左边或右边。
rvalue (pronounced "are-value"): An expression that is an rvalue may appear on the right- but not left-hand side of an assignment.
右值(发音为 are-value):右值只能出现在赋值的右边,不能出现在赋值语句的左边。
Variables are lvalues and so may appear on the left-hand side of an assignment. Numeric literals are rvalues and so may not be assigned. Given the variables:
变量是左值,因此可以出现在赋值语句的左边。数字字面值是右值,因此不能被赋值。给定以下变量:
int units_sold = 0;
double sales_price = 0, total_revenue = 0;
it is a compile-time error to write either of the following:
下列两条语句都会产生编译错误:
// error: arithmetic expression is not an lvalue units_sold * sales_price = total_revenue;
// error: literal constant is not an lvalue 0 = 1;
Some operators, such as assignment, require that one of their operands be an lvalue. As a result, lvalues can be used in more contexts than can rvalues. The context in which an lvalue appears determines how it is used. For example, in the expression
有些操作符,比如赋值,要求其中的一个操作数必须是左值。结果,可以使用左值的上下文比右值更广。左值出现的上下文决定了左值是如何使用的。例如,表达式
units_sold = units_sold + 1;
the variable units_sold is used as the operand to two different operators. The + operator cares only about the values of its operands. The value of a variable is the value currently stored in the memory associated with that variable. The effect of the addition is to fetch that value and add one to it.
中,units_sold 变量被用作两种不同操作符的操作数。+ 操作符仅关心其操作数的值。变量的值是当前存储在和该变量相关联的内存中的值。加法操作符的作用是取得变量的值并加 1。
The variable units_sold is also used as the left-hand side of
可以看出,左值和右值是跟左和右挂钩的。
另外:http://blog.youkuaiyun.com/lovewwy/archive/2009/02/26/3940670.aspx