When is it necessary to return a link from a function?

My question is: when does someone need to return an object by reference? This is due to the fact that we can also pass an object that must be filled through a list of parameters. Are there any specific scenarios in which it provides return by reference.

I ask for this regarding non-member functions.

Edit: I know about its use when overloading an operator.

+4
source share
4 answers

You need to return as a link if you want the opening end to have access to the specified object. Consider operator[] in the map and 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 saved object in this case to change it. The same thing happens with many other projects 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 specific value. You could force the copy to return by value, and the semantics would be the same, but at a higher cost. Or you can create a local variable and pass it by reference to a function that will fill it, and in this case you will not only bear the cost of copying, but also make the code more cumbersome:

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

Which, as you probably notice, is much more cumbersome in all applications of a member function.

Now I see that “I ask for this with functions other than members”, well, the same reasoning applies at different levels. Free constant functions can be applied to objects, consider:

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

The any_cast function is a free function, and yet it returns a link to another instance. If you need access to the actual object, not the copy, then the link is the solution. Please note that you do not need a link to read, and you can also return by value:

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

Similarly, in all cases when a function returns references to objects that are not arguments, but globals or static (returning a reference to a variable with automatic storage in a function is undefined behavior, but in all cases when it does it correctly, the semantics will not the same if you change it to any other solution.

+6
source

When you return a reference to an object that exists elsewhere, for example, the find function, which searches for a table.

+4
source

You can also use it for singleton (a static object that is created once and freed after main() . For example:

 struct A { unsigned int veryLongTable[ 1000 ]; }; const A& GetTable() { static A *table = NULL; if ( NULL == table ) { table = new A; // write values } return *table; } 
+1
source

Emphasis on mine in quotation marks

we can also pass an object

  • What if the default type is not constructive?
  • What if it is not constructive at all?
  • What if it is possible, but very expensive?

object to be filled

You were mistaken: the function returned by the value may be equivalent to a function that uses the output parameter, but not equivalent to the function that returns the link. To wit:

 T& f(); void f(T&); // We're not filling anything at all! f().some_member(); f( ??? ); 
0
source

All Articles