I don't understand when I would ever want to return a reference from a function (i.e. T& func(){...}
).
For example:
T& func() {
Something t;
return t;
}
This creates a Something
instance named t
, and then returns it by reference. When we go out of scope, t
is lost. So the reference is referring to 0
I think..?
So in other words, why would I ever want to return either a reference or a const reference? Please explain and give examples.