Chinaunix首页 | 论坛 | 博客
  • 博客访问: 54319
  • 博文数量: 11
  • 博客积分: 310
  • 博客等级: 一等列兵
  • 技术积分: 100
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-21 16:26
文章分类

全部博文(11)

文章存档

2014年(3)

2011年(1)

2010年(7)

我的朋友

分类: C/C++

2010-11-02 21:17:05

    Lvalues and rvalues are fundamental to C++ expressions. to put it  simply, an lvalue is an object reference and an rvalue is a value. The difference between lvalues and rvalues plays a role in the writing and understanding of expressions.

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.

Example 3-1. 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.html

C和C++ 字符串字面量的比较
C++ in a Nutshell

Linux C编程一站式学习


数组与指针---都是"退化"惹的祸



阅读(4896) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~