In C++, what does & mean after a function's return type?

C++

C++ Problem Overview


In a C++ function like this:

int& getNumber();

what does the & mean? Is it different from:

int getNumber();

C++ Solutions


Solution 1 - C++

It's different.

int g_test = 0;

int& getNumberReference()
{
     return g_test;
}

int getNumberValue()
{
     return g_test;
}

int main()
{
    int& n = getNumberReference();
    int m = getNumberValue();
    n = 10;
    cout << g_test << endl; // prints 10
    g_test = 0;
    m = 10;
    cout << g_test << endl; // prints 0
    return 0;
}

the getNumberReference() returns a reference, under the hood it's like a pointer that points to an integer variable. Any change applyed to the reference applies to the returned variable.

The getNumberReference() is also a left-value, therefore it can be used like this:

getNumberReference() = 10;

Solution 2 - C++

Yes, the int& version returns a reference to an int. The int version returns an int by value.

See the section on references in the C++ FAQ

Solution 3 - C++

Yes, it's different.

The & means you return a reference. Otherwise it will return a copy (well, sometimes the compiler optimizes it, but that's not the problem here).

An example is vector. The operator[] returns an &. This allows us to do:

my_vector[2] = 42;

That wouldn't work with a copy.

Solution 4 - C++

The difference is that without the & what you get back is a copy of the returned int, suitable for passing into other routines, comparing to stuff, or copying into your own variable.

With the &, what you get back is essentially the variable containing the returned integer. That means you can actually put it on the left-hand side of an assignment, like so:

getNumber() = 200;

Solution 5 - C++

int& getNumber(): function returns an integer by reference.

int getNumber(): function returns an integer by value.

They differ in some ways and one of the interesting differences being that the 1st type can be used on the left side of assignment which is not possible with the 2nd type.

Example:

int global = 1;

int& getNumber() {
        return global; // return global by reference.
}

int main() {

        cout<<"before "<<global<<endl;
        getNumber() = 2; // assign 2 to the return value which is reference.
        cout<<"after "<<global<<endl;

        return 0;
}

Ouptput:

before 1
after 2

Solution 6 - C++

The first version allows you to write getNumber() = 42, which is probably not what you want. Returning references is very useful when overloading operator[] for your own containers types. It enables you to write container[9] = 42.

Solution 7 - C++

"&" means reference, in this case "reference to an int".

Solution 8 - C++

It means that it is a reference type. What's a reference?

Wikipedia:

> In the C++ programming language, a reference is a simple reference datatype that is less powerful but safer than the pointer type inherited from C. The name C++ reference may cause confusion, as in computer science a reference is a general concept datatype, with pointers and C++ references being specific reference datatype implementations. The declaration of the form: > > Type & Name > > where is a type and is > an identifier whose type is reference > to . > > Examples: > > 1. int A = 5; > 2. int& rA = A; > 3. extern int& rB; > 4. int& foo (); > 5. void bar (int& rP); > 6. class MyClass { int& m_b; /* ... */ }; > 7. int funcX() { return 42 ; }; int (&xFunc)() = funcX; > > Here, rA and rB are of type "reference > to int", foo() is a function that > returns a reference to int, bar() is a > function with a reference parameter, > which is reference to int, MyClass is > a class with a member which is > reference to int, funcX() is a > function that returns an int, xFunc() > is an alias for funcX.

Rest of the explanation is here

Solution 9 - C++

It's a reference

Solution 10 - C++

It means it's returning a reference to an int, not an int itself.

Solution 11 - C++

It's a reference, which is exactly like a pointer except you don't have to use a pointer-dereference operator (* or ->) with it, the pointer dereferencing is implied.

Especially note that all the lifetime concerns (such as don't return a stack variable by address) still need to be addressed just as if a pointer was used.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNellerLessView Question on Stackoverflow
Solution 1 - C++sergiomView Answer on Stackoverflow
Solution 2 - C++Nick MeyerView Answer on Stackoverflow
Solution 3 - C++Tristram GräbenerView Answer on Stackoverflow
Solution 4 - C++T.E.D.View Answer on Stackoverflow
Solution 5 - C++codaddictView Answer on Stackoverflow
Solution 6 - C++fredoverflowView Answer on Stackoverflow
Solution 7 - C++jldupontView Answer on Stackoverflow
Solution 8 - C++ShayanView Answer on Stackoverflow
Solution 9 - C++CppLearnerView Answer on Stackoverflow
Solution 10 - C++SimonView Answer on Stackoverflow
Solution 11 - C++Ben VoigtView Answer on Stackoverflow