Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why assign a return value to a reference?

Tags:

c++

I'm looking over some code, and all the calls to a function returning a string are assigned to a reference. The function prototype something like:

 std::string GetPath(const std::string& top);

and it's used as

std::string& f = GetPath(cw);

or

 const std::string& f = GetPath(cw);

Why would one use a reference here instead of

 std::string f = GetPath(cw);
like image 390
Anonym Avatar asked Jan 13 '11 20:01

Anonym


People also ask

What is the use of return by reference?

Example: Return by Reference Unlike return by value, this statement doesn't return value of num , instead it returns the variable itself (address). So, when the variable is returned, it can be assigned a value as done in test() = 5; This stores 5 to the variable num , which is displayed onto the screen.

What is the advantage of returning a reference from the function?

The major advantage of return by address over return by reference is that we can have the function return nullptr if there is no valid object to return.

What does returns a reference mean?

It means you return by reference, which is, at least in this case, probably not desired. It basically means the returned value is an alias to whatever you returned from the function. Unless it's a persistent object it's illegal.

What is the difference between return by value and reference?

The major difference is that the pointers can be operated on like adding values whereas references are just an alias for another variable. Functions in C++ can return a reference as it's returns a pointer.


2 Answers

If the function returned a reference (which it doesn't) then you might want to assign the return value to a reference in order to keep "up to date" with any changes to that object. The reference returned would have to be to an object with a lifetime that extended beyond the end of the function.

Or (if the returned reference was not-const) because you wanted to keep a reference to the object to mutate it as a subsequent point. (If you wanted to mutate it immediately you would do it directly, no need to store the reference.)

As the function returns a value you could assign it to a const reference (to a non-const reference would be illegal) and extend the object's lifetime to the lifetime of the reference. However the effect would be exactly the same (const aside) as storing the value in a object directly.

Any thought that it might be less efficient may well prove unfounded and you can qualify the object with const if you want as well. (Most compilers eliminate the implied temporary and construct the return value in the object being initialized.)

As the object type is returned from the function by value it must be copyable so this is no reason to use a reference because of a concern that it isn't.

like image 118
CB Bailey Avatar answered Sep 23 '22 16:09

CB Bailey


This is probably an overzealous optimization aimed at cases where function being called returns a reference, as in:

const std::string& func(); ... const std::string& tmp = func(); ... 

to save on string copy.

like image 42
Nikolai Fetissov Avatar answered Sep 21 '22 16:09

Nikolai Fetissov