Consider the following code example:
SomeClass Callee() {
// Option 1:
return SomeClass(/* initializer here */);
// Option 2:
SomeClass tmp(/* initializer here */);
// Do something to tmp here
return tmp;
}
void Caller() {
SomeClass a(/* initializer here */);
SomeClass b = Callee();
SomeClass c(/* initializer here */);
}
AFAIK, b
will live longer than c
in the above example, but not longer than a
.
However, what happens if the return value of Callee()
is not assigned to any variable in Caller()
? Will the returned object behave like b
in the example above? Or will it be destructed before c
is created? I guess it's the latter, just want to be sure.
The code example is:
void Caller() {
SomeClass a(/* initializer here */);
Callee(); // what is the scope for the object returned by it?
SomeClass c(/* initializer here */);
}