The Coder

I am a humble coder.

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  4 随笔 :: 4 文章 :: 9 评论 :: 0 Trackbacks

下面这段引文可对左值、右值加深理解。

参考文献:《ANSI/ISO C++ Professional Programmer's Handbook》

lvalues and rvalues
An object is a contiguous region of storage. An lvalue is an expression that refers to such an object. The original
definition of lvalue referred to an object that can appear on the left-hand side of an assignment. However, const
objects are lvalues that cannot be used in the left-hand side of an assignment. Similarly, an expression that can appear
in the right-hand side of an expression (but not in the left-hand side of an expression) is an rvalue. For example

#include  < string >
using   namespace  std;
int &  f();
void  func()
{
    
int  n;
    
char  buf[ 3 ];
    n 
=   5 //  n is an lvalue; 5 is an rvalue
    buf[ 0 =   ' a ' //  buf[0] is an lvalue, 'a' is an rvalue
     string  s1  =   " a " , s2  =   " b " , s3  =    " c " //  "a", "b", "c" are rvalues
    s1  =   //  lvalue
    s2  + s3;  // s2 and s3 are lvalues that are  implicitly converted to rvalues
    s1  =   // lvalue
     string ( " z " );  // temporaries are rvalues
     int   *  p  =   new   int // p is an lvalue; 'new int' is an rvalue
    f()  =   0 // a function call that returns a reference is an lvalue
     s1.size();  // otherwise, a function call is an rvalue expression
}

An lvalue can appear in a context that requires an rvalue; in this case, the lvalue is implicitly converted to an rvalue.
An rvalue cannot be converted to an lvalue. Therefore, it is possible to use every lvalue expression in the example as
an rvalue, but not vice versa.
posted on 2006-06-02 12:03 TH 阅读(215) 评论(0)  编辑 收藏 引用

专题:Android  iPad jQuery Chrome OS

博客园首页  IT新闻  知识库  学英语  C++程序员招聘
标题  
姓名  
主页
验证码 *
内容(提交失败后,可以通过“恢复上次提交”恢复刚刚提交的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
[使用Ctrl+Enter键可以直接提交]
每天10分钟,轻松学英语
网站导航: