一个对象是被命名的存储区域或者被指向一块存储区域(an object is a named or pointed to region of storage);一个lvalue是一个指向对象的表达式(an lvalue is an expression referring to an object ).
重要的一句话就是“another way to determine whether an expression is an lvalue is to ask "can I take its address?". If you can, it’s an lvalue. If you can’t, it’s an rvalue.
加法操作一直返回一个rvalue,比如m+ 1 = n; 这就没法编译通过,因为 m + 1 这个表达式是一个rvalue。
下面例子
int main(int argc, char* argv[])
{
int obj = 0;
int* ptr = &obj;
int x = 1;
int* ptr2 = &(++x);
int* ptr3 = &(x++);
*&(++x) = 9;
*&(x++) = 10;
return 0;
}
int* ptr3 = &(x++); *&(x++) = 10; 两行报错,因为X++ 返回的结果是一个 rvalue;
而&右边的操作数必须为lvalue;
double &dr = 1; error , lvalue needed
constdouble &cdr = 1; right
the interpretation of last initialzation might be:
double temp = double(1); // first create a temporary with the right value
constdouble &cdr = temp;// then use the temporary as the initializer for cdr
本篇文章截取于:http://edu.codepub.com/2010/0524/22973.php
& the c++ programming language