c++ - 在 C++ 中延长临时值的生命周期

标签 c++ reference temporary

const int &ra=3;
  1. 据我所知,使 ra 为常量将延长临时 r 值的生命周期,在本例中为 3。据我所知,这有点令人困惑 ra 应该与 r-value 具有相同的地址,这里是 3,但 3 不是真正的变量,它没有存储它的内存。这怎么可能呢?

  2. 有什么区别:

    const int& ra=a;
    

    int& const ra=a;
    

最佳答案

but 3 is not a real variable and it does not have a memory where it's stored. so how can this be possible ?

实际上,一个临时对象是从文字 3 中创建的,然后该临时对象被绑定(bind)到 const 引用。这就是它成为可能的方式。


现在你的下一个问题:这两者之间的区别

const int& ra=a;
int& const ra=a;

是第二条语句不合法。

关于c++ - 在 C++ 中延长临时值的生命周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9589406/

相关文章:

java - 改变真实的字节数组Java

Java 合法前向引用

rust - 为什么 "the temporary is part of an expression at the end of a block"是一个错误?

c++ - 私有(private)和公共(public)运算符重载

c++ - Boost_program_option 链接器错误甚至存在 "boost_program_options"

c++ - C++ 中的超长类型,我可以为大型计算创建自己的超长类型吗?

reference - Xtext:对属性的 DSL 引用

c# - 在 Unity 中使用 DLL,MonoBehaviour

c++ - 临时对象销毁

sql-server - 临时表 - 允许的最大行值数为 1000