当我运行我的代码时,尝试返回一个局部变量:
#include<iostream>
using namespace std;
int &fun()
{
int x = 30;
return x;
}
int main()
{
fun() = 10;
cout << fun();
return 0;
}
为什么有些编译器输出0而有些编译器为30
答案 0 :(得分:4)
返回对随后超出范围的局部变量的引用是C ++中未定义的行为。
有时它可能会起作用,有时可能不起作用。偶尔编译器可能会吃你的猫。