Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implications of using an ampersand before a function name in C++?

Given the example:

inline string &GetLabel( ) {
        return m_Label;
};

Where m_Label is a private class member variable.

The way I think I understand it, this function will return a reference to the variable m_Label. What would be the implications of using this throughout my program and would it be a better to just return the value, instead of the reference? Thank you!

like image 793
Cuthbert Avatar asked Dec 22 '11 22:12

Cuthbert


1 Answers

It returns a reference to the private member.

There are many cases where this is desirable, but some care should be taken.

IMO it's generally not a good idea to return a copy of an internal object that is not an integral type, for overall performance reasons. Yes I know, premature optimization is not good, but this is not really optimization, it's just a good performance practice that allows the caller to determine the performance implications; if it wants a copy, it can just not declare the variable that it's assigning it to as a reference.

There are 2 general rules of thumb I use here:

1) If you don't want the caller to be able to modify the private object directly, declare the return value as a const reference:

inline const string& GetLabel() const{ return m_Label; }

2) A caller should never store the reference returned from a class method, it should only be used locally where the parent object is guaranteed to be in scope.

If for some reason you need callers to be able to store a reference to your internal objects, use smart pointers instead.

like image 72
Gerald Avatar answered Oct 03 '22 13:10

Gerald