To clarify, we can show 3 scripts for testClass create() :
1
Return a copy, but its search by constant link
testClass create() { return testClass(); } testClass const &obj = create();
It extends the lifetime of a temporary testClass() , while obj .
2
Returning a copy and intercepting it by assignment (RVO)
testClass create() { return testClass(); } testClass obj = create();
It extends the life of the temporary testClass() as long as obj , since RVO is implicitly applied to it. Better to say, in fact, there is no temporary object, everything works on obj even in create() .
3
Return a copy and bind it using assignment (without RVO)
testClass create() { return testClass(); } testClass obj = create();
The lifetime of the temporary testClass() longer after returning from create() , and a new object comes into the world.
source share