What does it mean to return a reference?

14,969

Solution 1

It means you return by reference, which is, at least in this case, probably not desired. It basically means the returned value is an alias to whatever you returned from the function. Unless it's a persistent object it's illegal.

For example:

int& foo () {
    static int x = 0;
    return x;
}

//...
int main()
{
    foo() = 2;
    cout << foo();
}

would be legal and print out 2, because foo() = 2 modifies the actual value returned by foo.

However:

int& doit () {
    int x = 0;
    return x;
}

would be illegal (well, accessing the returned value would), because x is destroyed when the method exits, so you'd be left with a dangling reference.

Returning by reference isn't common for free functions, but it is for methods returning members. For example, in the std, the operator [] for common containers return by reference. For example, accessing a vector's elements with [i] returns an actual reference to that element, so v[i] = x actually changes that element.

Also, I hope that "is essentially equal to this code" means that they're semantically sort of (but not really) similar. Nothing more.

Solution 2

It means that you return a pointer to the memory address where the correspondent data is, instead of the very data.

Solution 3

Assuming this code (to make it comparable to the first example) :

int main (void) {
  int* foo = /*insert useful place in memory*/;
  *foo = doit(foo);
}

int& doit (int* value) {
  *value = 24;
  return *value;
}

The int& is not really useful as a return type in this case, because it provides access to the variable in memory (of which you pass the pointer to the function).

Does it return a pointer to an int? Or would it just return an int?

No, it returns a reference to an int. If you want, you can look at it as a pointer which can not be nullptr.

Share:
14,969
crodriguez
Author by

crodriguez

Updated on June 08, 2022

Comments

  • crodriguez
    crodriguez almost 2 years

    I understand the concept of references in C++, and I understand what they do when used in function parameters, but I am still very much confused on how they work with return types.

    For example, when used in parameters, this code:

    int main (void) {
      int foo = 42;
      doit(foo);
    }
    
    void doit (int& value) {
      value = 24;
    }
    

    is similar to this code:

    int main (void) {
      int foo = 42;
      doit(&foo);
    }
    
    void doit (int* value) {
      *value = 24;
    }
    

    (knowing that the compiler will automatically put an asterisk in front of value every time it is used in the first code sample of doit, but in the latter you'd have to put the asterisk in yourself every time you try to use value)

    So when used as a reference what does this next code (using reference in a return type) translate to? Does it return a pointer to an int? Or would it just return an int?

    int main (void) {
      int* foo = /*insert useful place in memory*/;
      foo = doit(foo);
    }
    
    int& doit (int* value) {
      //insert useful code
    }
    
    • johnathan
      johnathan over 11 years
      It would return a reference to an int, A reference you can think of as a named pointer, but without all the raw power of a pointer. The big problem with all of that , returning a reference to an object that quite possibly is created on the stack is that your going to have some nasty memory issues when the function returns and the functions stack is cleaned up. So , just be aware that just because it's a reference, that dose NOT mean that you wont have memory issues, and with references, it's really worse because the memory issue is hidden.
  • crodriguez
    crodriguez over 11 years
    Thanks Luchian, that helped a lot! And yes, I meant "similar". I'll edit that for future references.
  • crodriguez
    crodriguez over 11 years
    Just to help clear up, return by reference returns the address of whatever object you attempted to return. You obviously don't want to return the address of an object with local scope because the object will be destroyed when the function called is done, which will leave you with a dangling reference (which would in turn may cause a use-after-free bug). Therefore, a good use of return by reference would be, as Luchian said, when returning a member.
  • Luchian Grigore
    Luchian Grigore over 11 years
    @MrMeganFox returning by reference doesn't return the address, it returns an alias.
  • Chris Dodd
    Chris Dodd over 8 years
    @LuchianGrigore: no it returns a reference, not an alias. References are usually implemented by using machine addresses under the hood.
  • Luchian Grigore
    Luchian Grigore over 8 years
    @ChrisDodd a reference is an "alias" of the object it refers to - a different name for the same object.
  • 1stCLord
    1stCLord over 5 years
    The object doesn't need to be persistent, the lifetime of the object needs to be greater than the function scope, which can, for example be via lifetime extension.