kur*_*uro 4 c++ lifetime const-reference temporary-objects
考虑以下代码 -
#include <iostream>
#include <stdio.h>
const int & retRef() {
return 6;
}
int main()
{
const int& k = retRef();
printf("Value: %d\n", k);
printf("Address: %p\n", &k);
printf("Value: %d\n", k);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是 -
Value: 6
Address: 0x7ffd45bf544c
Value: 32692
Run Code Online (Sandbox Code Playgroud)
为什么在打印变量的地址后值发生了变化k?如果我更换线const int& k = retRef()用const int& k = 6;的输出为预期.
为什么这种行为不同?提前致谢
| 归档时间: |
|
| 查看次数: |
127 次 |
| 最近记录: |