爲什麼此代碼可以在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