As everyone else says, do not do this. Returning a link or pointer to a local variable is always incorrect, because the return act gets rid of the local variable and, therefore, the link or pointer is automatically invalid.
Copying may not be a problem. C ++ compilers are allowed to skip copy constructors when returning from functions ("return value optimization"), so a reasonably intelligent compiler can create a value in place. This way you can return a lot of value without copying. Try and see; you can temporarily output output statements in the copy constructor (if you wrote one and don't use the automatically generated one) to see if it was actually called.
So, without starting and trying, you do not know if the actual copying is taking place, and if so, then what is the problem. As always, time and profile are run to see if there is a problem, and if so, where. Doing something risky and / or confusing to speed up performance is almost never worth doing before synchronization and profiling.
David thornley
source share