Hi, from my understanding, if you initialize an object without the "new", it will be only saved in the stack. If the method exits, the object is destroyed. Is that correct? What about this code?
int main() {
string xxx = getText();
cout << "from main() " << xxx << endl;
}
string getText() {
string text("Hello World!");
cout << "from getText() " << text << endl;
return text;
}
output is
from getText() Hello World!
from main() Hello World!
In getText() method, I created a string without the "new", so I assumed that the string will be destroyed after the return. But why does xxx in main() equal to the return of the getText() method? Should it be that xxx contain an unknown value since the object it points to was destroyed? Thanks a lot!