I have the following code:
using namespace std;
vector<string*> v;
{
string s = "hello";
v.push_back(&s);
}
{
string ss = "goodbye";
v.push_back(&ss);
}
cout << v.at(0)->c_str() << endl;
cout << v.at(1)->c_str() << endl;
which prints
goodbye
goodbye
if I remove the area bracket, the code will print
hello
goodbye
What exactly happens when I leave the first scope that the pointer to the first line now points to the second?
source
share