Hi all,
Is it OK to return a reference to a local variable from a function? By local I mean that the variable would be created(on the stack i.e. without using new) within the function and its scope is within that function only. I got contradictory answers when I searched about this. 1) says that kind of usage is correct but 2) contradicts it.
1) http://functionx.com/cpp/examples/returnreference.htm
2) http://www.cprogramming.com/tutorial/references.html (under References & Safety section)
Which of them is right?
Another question I had is, if 1) is right then are the following serve same purpose.
i) int& a = func();
ii) int a = func(); where func() returns a reference to an int (local variable in that function).
In both the above cases, there is no copying of return value involved isn't it. I would like to prevent copying of return values since the return value could be large.
Thank you in advance.
Raghava.