lvalue & const

一个对象是被命名的存储区域或者被指向一块存储区域(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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值