非const引用和const引用 [英] non-const reference and const reference

查看:75
本文介绍了非const引用和const引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


这是我对非const引用,const引用及其与lvalue / rvalue的关系的理解。请帮助检查是否正确,并随时纠正我。谢谢。


1. const引用可以绑定到rvalue,例如临时对象。而生命和生命保证临时对象的扩展,我们可以通过const-reference安全地操作。


2.非const引用不能绑定到rvalue,我想原因rvalue是不可寻址的?我们不能通过它的参考改变右值?还有其他原因吗?我不太确定我的理解是否完全正确。由于存在一些不可修改的左值(因此我们并不总是需要通过其引用来修改值)。我还在研究编译器本质上是什么原因为什么非const引用不能绑定到右值。


3. const和非const引用都可以绑定到了一个左值。



提前谢谢,

George

Hello everyone,


This is my understanding of non-const reference, const reference and their relationships with lvalue/rvalue. Please help to review whether it is correct and feel free to correct me. Thanks.

1. A const reference can be binded to a rvalue, for example, a temporary object. And the "life" of the temporary object is guaranteed to be extended and we can safely operate through the const-reference.

2. A non-const reference can not binded to a rvalue, I think the reason is rvalue is not addressable? And we can not change the rvalue through its reference? Are there any other reasons? I am not quite sure whether my understanding is fully correct. Since there are some non-modifiable lvalues (so we do not always need to modify values through its reference). I am still studying what is the reason in essence in compiler why a non-const reference can not be binded to a rvalue.

3. Both const and non-const reference can be binded to a lvalue.


thanks in advance,
George

推荐答案

我不知道你在说什么
I''m not sure what you are talking about:
展开 | 选择 | Wrap < span class =codeDivider> | 行号


感谢weaknessforcats,



你的回复很棒!但我无法看到你对我原来的三个问题的直接回答。 :-)

Thanks weaknessforcats,


Your reply is great! But I can not see your direct answer to my original 3 questons. :-)


我不确定你在说什么
展开 | 选择 | Wrap | 行号


代码示例是不够的?你还想让我说什么?
The code sample isn''t enough?? What else would you like me to say?


这篇关于非const引用和const引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆