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