Consider the following:
#include <iostream>
struct X
{
int x;
X() { x = 6; }
};
X val()
{
return X();
}
int main()
{
X & x = val();
X x2 = x;
std::cout << x.x << " " << x2.x;
}
What is the lifetime of the object returned by the call to val()? I
would have thought it should be destroyed immediately, creating a
dangling reference, yet the program works, does the returned object
live until the end of it's enclosing scope?
#include <iostream>
struct X
{
int x;
X() { x = 6; }
};
X val()
{
return X();
}
int main()
{
X & x = val();
X x2 = x;
std::cout << x.x << " " << x2.x;
}
What is the lifetime of the object returned by the call to val()? I
would have thought it should be destroyed immediately, creating a
dangling reference, yet the program works, does the returned object
live until the end of it's enclosing scope?
Comment