Is the link returned useless?

Based on the background of C #, I was more or less puzzled by the seemingly strange behavior of handling the return method in C ++. Now my problem is the method in C ++, returning by reference is not a very useful method, because ... unlike C #, any variable declared inside the method body will go out of scope after the control completes this method.

So in C ++ this one can't even compile (but the equivalent version in C # can):

int& DoubleValue(int nX) { int nValue = nX * 2; return nValue; // return a reference to nValue here } // nValue goes out of scope here 

The only time that returning by reference is useful is when you return a link to an existing data element in the class or return a link to an element inside a method parameter. But in both cases there is no need to return anything; since the returned link is already freely available to the calling method.

So, my conclusion is that there is no need to use return by reference at all. I'm right?

+7
source share
7 answers

The only time that returning by reference is useful is when you return a link to an existing data element in a class or return a link to an element inside a method parameter.

In other words, true. This variable cannot be “belonging” to the class (as in relation to its lifetime): it could be indicated to the class by some earlier function call or global / singleton, known to the class, but not being part of it, or even a newly allocated area in the shared memory or heap (although returning the link, not the pointer, assumes that ownership is not granted to the caller), but in the end the class should have some access to this data.

But in both cases there is no need to return anything; since the returned link is already freely available to the calling method.

No, because objects can have private and protected members and provide friendship to other classes or functions, it is therefore quite possible that the called function can access (and therefore return the link) some data that the caller does not have direct access to.

In addition, many functions find a specific variable to do some work, and then return a link to it. If the caller needed to make a separate call to find this variable again, it can be inefficient (as well as verbose in the calling code).

So, my conclusion is that there is no need to use return by reference at all. I'm right?

No ... due to the erroneous premise above.

Another unnecessary but convenient use of return by reference is illustrated by typical streaming functions:

 std::ostream& operator<<(std::ostream& os, const X& x) { return os << x.str(); } 

Above, the link to the tool says ...

 std::cout << x << y; 

... rated as ...

 (std::cout << x) << y; operator<<(operator<<(std::cout, x), y) 

That all the chains together are beautiful. Similarly:

 while (std::cin >> x >> y) ... 

... works not only because of the chaining for consecutive entries in x and y, but also because std :: cin is still available for evaluation in a boolean context that ultimately calls another member function, effectively asking operations.

+5
source

Operations like std::vector<>::operator[] return links to elements obtained by dereferencing the pointer (and adding offsets to it). The pointer in this case is a private member, internal to vector , and therefore the user could not get himself without violating the class abstraction.

+11
source

It is true that this does not work to return a link to something allocated on the stack. It just can't work.

However, returning by reference can be extremely useful - for example, the collection indexing operator can return a value by reference, which allows you to assign this element to the collection.

+5
source

I just wanted to mention that in addition to returning links to private / secure data members and operator chains, there are metaprogramming methods for templates that use functions returned by reference to temporary variables. This can have a performance boost, but obviously this should be done with caution. The Boost.Proto library is one example of expression template methods that use references to time sections to avoid creating time series. I'm sorry that I can’t give a simple example of this ... I'm not sure I understand this myself, but I just wanted to point out that even the most illogical things in C ++ are still allowed, and if they are, someone will find a way to use it wisely. And this includes returning the link to a temporary one.

+1
source

Try it.

 int & DV(int & nX) { return nX *= 2; } // . . . int i = 2; int & j = DV(DV(i)); 
0
source

Hope other experts come up with a more detailed explanation. I'm just trying to say a few cases where C / C ++ programmers use reference types.

The C ++ reference type is simply a syntactic improvement over the old C pointer type. In C / C ++, all parameters and return values ​​are value types. That is, the values ​​are copied to / from the calling party / called party. Apparently, this is not always the desired operation. Therefore, when C / C ++ programmers want to prevent unnecessary copying, they use pointers or references.

  • To pass a large value
  • To allow the caller / callee to change the value

Okay .. any other?

0
source

The usual goal of returning by reference is that assignment chains can be made as follows:

 MyVar1 = MyVar2 = Myvar3 = MyVar4; 
0
source

All Articles