Returning reference to a local variable
Why can this code run successfully in Code::block. The IDB just reports warning: "reference to local variable ‘tmp’ returned", but ouput the result "hello world" successfully. #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; } prvit Maybe this link will help you. Upon leaving a function, all local variables are destroyed. By returning a reference to tmp , you are returning a reference to an object that soon ceases to exist (that is,