注册 | 登录 忘记密码? 51cto首页 | 博客 | 论坛 | 招聘
热点文章 科普系列之-使用Windows的..
 帮助

C和C++里面的lvalue 和 rvalue的释义


2006-09-28 11:39:58
 标签:c++   [推送到技术圈]

     在看gcc的文档的时候,看到一个词lvalue,查了金山词霸其释义为 lvalue [计] 左值。因为的确在介绍编译原理的课程中听过这个词,大致知道其意思就没有多想。但是看完gcc文档的这个篇幅,都无法明白全篇在说什么。
 
    问题还是出在了lvalue这个词的“左值”是什么意思的理解上了。再找m-w字典,却告知没有这个词。于是google了一把,的确很多地方都称其为左值,我仍然不得要领。最后在一个百科网站http://www.about.com/"'" target="_blank">about site上找到该词的准确释义,摘贴如下:
 
 
   definiti c and c++ have the notion of lvalues and rvalues associated with variables and constants. the rvalue is the data value of the variable, that is, what information it contains. the "r" in rvalue can be thought of as "read" value. a variable also has an associated lvalue. the "l" in lvalue can be though of as locati meaning that a variable has a location that data or information can be put into. this is c with a constant. a c has some data value, that is an rvalue. but, it cannot be written to. it does not have an lvalue.

   another view of these terms is that objects with an rvalue, namely a variable or a c can appear on the right hand side of a statement. they have some data value that can be manipulated. objects with an lvalue, such as variable, can appear on the left hand side of a statement. an object must be addressable to store a value.

here are two examples.

int x;

x = 5; // this is fine, 5 is an rvalue, x can be an lvalue.
5 = x; // this is illegal. a literal c such as 5 is not
      // addressable. it cannot be a lvalue.

 

 
   这段就说的很明白 lvalue中的l其实指的表示该值的存储地址属性,而另外一个相对的词rvalue值中的r指得是read的属性,和左右根本没有任何关系。金山词霸的解释真是狗屎啊。




    文章评论
 
2006-09-28 13:31:56
不好意思了哈!
兄弟啊,借你的banner用用,呵呵!

2007-10-20 19:41:28
其实,之所以把它称为左值,我想也应该有它的道理,因为在等号左边的值是能够被赋值的,它拥有自己的存储地址,而且属性是可写的。
人们可能为了简化其称呼,将其称为左值,但其真正的含义却在于:a variable has a location that data or information can be put into。
在我们编程的时候,什么情况下(或者说什么语句下)我们可以知道一个变量是否为lValue呢?我想只有在被赋值的时候,才能够体现出它的属性,我们才能够判别吧。
所以,“a variable has a location that data or information can be put into。”简写成lValue,中文称为左值,但他的解释并不是“left value”。
——以上纯属个人观点。

 

发表评论

昵   称:
验证码:  点击图片可刷新验证码  博客过2级,无需填写验证码
内   容: