2

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.

Aviv Cohn
  • 15,543
  • 25
  • 68
  • 131
  • 2
    Its not uncommon to return a reference to a static variable defined in a function. Also member functions often return references. Whenever the reference is to an object that lasts longer than the function if could make sense. – Galik Oct 05 '14 at 23:42
  • Another example is max. Pay careful attention to the return type in the sample implementation http://www.cplusplus.com/reference/algorithm/max/ – Neil Kirk Oct 05 '14 at 23:51
  • How would the reference magically start referring to `0`? What does that even mean? – Kerrek SB Oct 06 '14 at 00:04
  • See: [Move semantics in C++ 11](http://stackoverflow.com/questions/3106110/what-are-move-semantics) - usually a better away to go. – Vector Oct 06 '14 at 00:10
  • The object would go out of scope, and the pointer would be a dangling pointer (dangling reference?). But dangling pointers don't magically get assigned to nullptr, and there are no null references even if they did. – Kevin Oct 06 '14 at 02:35
  • But that's only if you're returning a reference to a local. You might be returning a reference or const reference to a data member of a class, which will stay in scope as long as the object does. Or a reference to a global / static global. Or you might even return one of your parameters, (such is common with operator<< and operator>>). – Kevin Oct 06 '14 at 02:37

4 Answers4

3

You often do it when you are returning a member from one of the function's arguments. An example from the standard library:

template<class T, size_t n>
struct array
{
    T data[n];

    T& operator[](size_t i) { return data[i]; }
};
o11c
  • 15,265
  • 4
  • 50
  • 75
1

Sometimes you return a reference to a static variable defined in a function:

T& func() {
    static Something t; // this will exist until program closes
    return t;
}

That's a technique to help combat problems involving the order of static initialization.

Obviously member variables can make sense (I'm sure you're not asking about those though):

T& MyType::func() {
    return this->t; // persists with the object
}

Also sometimes you want to return the object that was passed in to the function:

std::ostream& operaor<<(std::ostream& os, const MyType& t) {
    // blah blah
    return os; // pass it back out
}

That enables you to chain functions together like:

if(std::getline(input >> size >> std::ws, line))
{
    // size and line were successfully read here
}
Galik
  • 47,303
  • 4
  • 80
  • 117
0

You can think of references as little more than pointers. Any time you'd return a pointer from a function, you might return a reference type instead.

I think your issue with returning references is the issue of scope. As long as there are no scope issues, it's just another tool at your disposal.

tenfour
  • 36,141
  • 15
  • 83
  • 142
0

Returning a reference to the current object is used for implementing a user-defined assignment operator.

class Foo
{
  Foo& operator=(const Foo&);
  //...
};

It is also used as a return type when implementing overloads of operators +=, -=, *=, etc. These operators suggest that the return type is the adjusted current object, not a new object. Therefore to return the current object, you return a reference to *this.

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45