为什么此代码可以在Code :: block中成功运行。美洲开发银行刚刚报道返回对本地变量的引用
警告: “参照局部变量‘TMP’返回”,
但输出中的结果 “Hello World” 的成功。
#include <iostream>
#include<string>
using namespace std;
const string &getString(const string &s)
{
string tmp = s;
return tmp;
}
int main()
{
string a;
cout<<getString("hello world")<<endl;
return 0;
}
[返回局部或临时变量的地址]的可能重复(http://stackoverflow.com/questions/2744264/returning-the-address-of-local-or-temporary-variable) – iammilind