Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a good idea to always return references for member variable getters?

If I have a class that has many int, float, and enum member variables, is it considered efficient and/or good practice to return them as references rather than copies, and return constant references where no changes should be made? Or is there a reason I should return them as copies?

like image 957
Jengerer Avatar asked Sep 19 '10 05:09

Jengerer


People also ask

When should we return by reference?

A C++ function can return a reference in a similar way as it returns a pointer. When returning a reference, be careful that the object being referred to does not go out of scope. So it is not legal to return a reference to local var. But you can always return a reference on a static variable.

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

Functions can be declared to return a reference type. There are two reasons to make such a declaration: The information being returned is a large enough object that returning a reference is more efficient than returning a copy.

What are reference variable and how they can be used for return by 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. When function returns a reference it means it returns a implicit pointer.

What is meant by return by reference?

Example: Return by Reference In program above, the return type of function test() is int& . Hence, this function returns a reference of the variable num . The return statement is return num; . Unlike return by value, this statement doesn't return value of num , instead it returns the variable itself (address).


4 Answers

There is no reason to return primitive types such as int and float by reference, unless you want to allow them to be changed. Returning them by reference is actually less efficient because it saves nothing (ints and pointers are usually the same size) while the dereferencing actually adds overhead.

like image 94
casablanca Avatar answered Sep 20 '22 03:09

casablanca


If they are constant references, maybe it is OK. If they are not constant references, probably not.

As to efficiency - on a 64-bit machine, the references will be 64-bit quantities (pointers in disguise); int and float and enum will be smaller. If you return a reference, you are forcing a level of indirection; it is less efficient.

So, especially for built-in types as return values, it is generally better to return the value rather than a reference.

like image 24
Jonathan Leffler Avatar answered Sep 19 '22 03:09

Jonathan Leffler


Some cases it is necessary:

Look at overloaded operator[] for any class. It usually has two versions. The mutating version has to return a reference.

int &operator[](int index);           // by reference
int operator[](int index) const;      // by value

In general, It is OK to allow access to class members by trusted entities by a class e.g. friends. In case these trusted entities also need to modify the state, references or pointers to the class members, are the only options one has.

In many cases, references usually simplify syntax e.g where 'v' is STL vector.

v.at(1) = 2 vs *(v.at(1)) = 2;
like image 30
Chubsdad Avatar answered Sep 19 '22 03:09

Chubsdad


This is probably mostly a matter of style or preference. One reason to not return references is because you are using getters and setters to allow you to change the implementation of those members, If you changed a private member to another type, or removed it completely because it can be computed, then you no longer have the ability to return a reference, since there's nothing to reference.

On the other hand, returning references for non-trivial types (compound classes) can speed up your code a bit over making a copy, and you can allow those members to be assigned through the returned reference (if desired).

like image 26
SingleNegationElimination Avatar answered Sep 18 '22 03:09

SingleNegationElimination