Hi,
In C++ what is the difference between return by value and return by reference in the function?
When we need to use return by value and return by reference in the function?
Thanks.
Hi,
In C++ what is the difference between return by value and return by reference in the function?
When we need to use return by value and return by reference in the function?
Thanks.
As per you suggested link, if we should not return reference of local variable in the function, then what is the use of returning reference , we can return the value itself. I could not understand , please help.
Remember that a reference is essentially just a pointer.
You should never return a reference to a local variable because it won't exist once the function has existed.
Returning a reference to something should only be done with careful consideration... the lifetime of what you're referencing needs to exceed the lifetime/use of the returned reference.
Generally it's only of use if copying the returned item is expensive, and you're sure your normal use of it won't break the lifetime issue.
12 people are following this question.