5.8.  Returning References from Functions

[ fromfile: functions.xml id: referencereturns ]

  cout << thing1 << thing2 << thing3 ... ;

Example 5.15. src/reference/maxi.cpp

#include <iostream>
using namespace std;
int& maxi(int& x, int& y) { 

    return (x > y) ? x : y;
}

int main() {
    int a = 10, b = 20;
    maxi(a,b) = 5;      1
    maxi(a,b) += 6;     2
    ++maxi(a, b) ;      3
    cout << a << '\t' << b << endl;
    return 0;
}
Output:

17      5


1

Assigns the value 5 to b.

2

Increases a by 6. a is now 16.

3

Increments a by 1.


[Warning]Caution

Be careful that your function does not return a reference to a temporary (local) object. A moment's thought should make the reason for that restriction clear: When the function returns, its local variables are all destroyed.

int& max(int i,int j) {
    int retval = i > j ? i : j;
    
    return retval;
}
 badmax.cpp:4: warning: reference to local variable 'retval' returned

A more practical example showing the benefits of reference returns is shown in Example 5.16, which defines some common operators for vectors.