Cuthbert
Cuthbert

Reputation: 2998

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!

Upvotes: 14

Views: 13226

Answers (5)

Gerald
Gerald

Reputation: 23499

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.

Upvotes: 10

thesamet
thesamet

Reputation: 6582

One implication is that if the enclosing object is destructed, the reference becomes invalid:

Object* o = new Object;
string& label = o->GetLabel();
delete o;
// label becomes a dangling reference here.

Another implication is that a caller may modify the string. You can remedy that by returning a const reference.

Upvotes: 2

Mark Ransom
Mark Ransom

Reputation: 308538

Returning a reference means that the calling code can modify the value of your member variable after you return. That's very dangerous, unless you intended for that to happen.

Better is a const reference, or return by value (without the &).

Upvotes: 2

AJG85
AJG85

Reputation: 16217

You're are correct. It's a reference to the string member.

The implication will be that if a caller were to assign a value or otherwise modify the returned string that they would also be modifying the member variable. If this is not the intent you may want to return a copy by value to avoid breaking encapsulation.

Upvotes: 1

Fred Larson
Fred Larson

Reputation: 62123

The ampersand isn't before the function name so much as it's after the return type. it returns a reference to a string.

The implication of this is that a caller of this function could modify the value of m_label through the reference. On the other hand, it avoids copying the string. You might want the reference and the function to be const, like so:

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

Best of both worlds. You avoid the copy, but callers can't change your object.

Upvotes: 14

Related Questions