Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is it required to return a reference from a function?

Tags:

c++

My question is when does any one need to return an object by reference? Well this comes from the fact that we can as well pass the object to be filled through the parameter list itself. Are there any particular scenarios where it mandates the return by reference.

I'm asking this with respect to non member functions.

Edit: I'm aware of its usage in operator overloading.

like image 534
Purnima Avatar asked Dec 16 '22 12:12

Purnima


2 Answers

You need to return as reference when you want the reveiving end to have access to the referred object. Consider operator[] in a map and the use case:

std::map<std::string, int> word_count;
std::string word;
while ( std::cin >> word ) {
   word_count[ word ]++;
}

You do not want to extract the value from the map, but rather access the stored object in this case to modify it. The same goes with many other designs, where you need access to some internal data, but you do not want to copy it:

if ( person.name() == "Pete" ) {

The user does not need to copy the object, only to check whether the object has a concrete value. You could have forced a copy by returning by value, and the semantics would be the same, but with higher costs. Or you could create a local variable and pass it by reference to a function that will fill it, in which case you are not only incurring the cost of copying, but also making code more cumbersome:

std::string name;
person.fill_name( name );
if ( name == "Pete" ) {

Which, as you can probably notice, is much more cumbersome in all uses of the member function.

Now, I see the "I'm asking this with non-member functions", well, the same rationale applies at different levels. Free standing functions can be applied to objects, consider:

boost::any any = 5;
boost::any_cast<int&>( any )++;

The function any_cast is a free function and yet it returns a reference to another instance. If you need access to the actual object and not a copy, then a reference is the solution. Note that for only reading you don't need a reference and you may as well return by value:

std::cout << boost::any_cast<int>( any );   // will print 6 now

Similarly in all cases where the function returns references to objects that are not arguments, but globals or static (returning a reference to a variable with auto storage in a function is undefined behavior, but in all cases where it is correct to do so the semantics would not be the same if you change it with any other solution.

like image 105
David Rodríguez - dribeas Avatar answered Dec 19 '22 03:12

David Rodríguez - dribeas


When returning a reference to an object that exists elsewhere, for example a find function searching a table.

like image 29
Bo Persson Avatar answered Dec 19 '22 02:12

Bo Persson