为什么对局部变量的引用仍然有效 我在C++程序中运行以下简单程序: #include <iostream> using namespace std; string& ShowString() { string s1 = "abcd"; return s1; } int main() { string s2 = ShowString(); cout << s2 << endl; return 0; } #包括 使用名称空间std; string&ShowString() { 字符串s1=“abcd”; 返回s1; } int main() { 字符串s2=ShowString(); 不能
因为这是一种未定义的行为。你不能相信这个值为什么对局部变量的引用仍然有效 我在C++程序中运行以下简单程序: #include <iostream> using namespace std; string& ShowString() { string s1 = "abcd"; return s1; } int main() { string s2 = ShowString(); cout << s2 << endl; return 0; } #包括 使用名称空间std; string&ShowString() { 字符串s1=“abcd”; 返回s1; } int main() { 字符串s2=ShowString(); 不能,c++,C++,因为这是一种未定义的行为。你不能相信这个值