分类: C/C++
2010-11-02 21:17:05
An lvalue is an expression that yields an object reference, such as a variable name, an array subscript(下标) reference, a dereferenced (解引用' *')pointer, or a function call that returns a reference. An lvalue always has a defined region of storage, so you can take its address.
An rvalue is an expression that is not an lvalue(不是左值的表达式就是右值). Examples of rvalues
include literals(字符串字面值(string literal)在c中不是编译期间常量,而是运行期间常量,因为它不被归纳为 c中常量中,它是不可修改的左值。) the results of most operators, and function calls
that return nonreferences. An rvalue does not necessarily have any
storage associated with it.(右值强调的是值,如果一个变量是左值,也可以作为右值,不过要经过左值到右值的转换 .)
Strictly speaking(严格地说,函数名也是一个不可修改的左值), a function is an lvalue, but the only uses for it are to use it in calling the function, or determining the function's address. Most of the time, the term lvalue means object lvalue, and this book follows that convention.
C++ borrows the term lvalue from C, where only an lvalue can be used on the left side of an assignment statement. The term rvalue is a logical counterpart for an expression that can be used only on the righthand side of an assignment. For example:
#define rvalue 42
int lvalue;
lvalue = rvalue;
In C++, these simple rules are no longer true, but the names remain because they are close to the truth. The most significant departure from traditional C is that an lvalue in C++ might be const, in which case it cannot be the target of an assignment. (C has since evolved and now has const lvalues.)
The built-in assignment operators require an lvalue as their lefthand operand. The built-in address (&) operator also requires an lvalue operand, as do the increment (++) and decrement (--) operators. Other operators require rvalues. The rules are not as strict for user-defined operators. Any object, including an rvalue, can be used to call member functions, including overloaded =, &, ++, and -- operators.
Some other rules for lvalues and rvalues are:
An array is an lvalue, but an address is an rvalue.
The built-in array subscript ([]), dereference (unary *), assignment (=, +=, etc.), increment (++), and decrement (--) operators produce lvalues. Other built-in operators produce rvalues.
A type cast to a reference type produces an lvalue; other casts produce rvalues.
A function call (including overloaded operators) that returns a reference returns an lvalue; otherwise, it returns an rvalue.
An lvalue is converted implicitly to an rvalue when necessary, but an rvalue cannot be implicitly converted to an lvalue.
shows several different kinds of lvalues and rvalues.
class number {
public:
number(int i = 0) : value(i) {}
operator int( ) const { return value; }
number& operator=(const number& n);
private:
int value;
};
number operator+(const number& x, const number& y);
int main( )
{
number a[10], b(42);
number* p;
a; // lvalue
a[0]; // lvalue
&a[0]; // rvalue
*a; // lvalue
p; // lvalue
*p; // lvalue
10; // rvalue
number(10); // rvalue
a[0] + b; // rvalue
b = a[0]; // lvalue
}
关于左值和右值更详细的说明请仔细阅读下面几篇文章。
C++的左值和右值http://blog.csdn.net/ivymuse/archive/2010/08/04/5785553.aspx
理解左值与右值 http://blog.chinaunix.net/u/13991/showart_111414.htmlC++ in a Nutshell
C和C++ 字符串字面量的比较