13

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!

Cuthbert
  • 2,908
  • 5
  • 33
  • 60

5 Answers5

14

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.

Fred Larson
  • 60,987
  • 18
  • 112
  • 174
  • Indeed, you probably want _both_ the const and non-const versions. – ildjarn Dec 22 '11 at 23:19
  • @ildjam That is only partly true. Usually you will only want to provide the const version, unless you have a reason to want to allow callers to modify the object directly. If you do provide the non-const version, then you should also provide the const version, so that you can still get read-only access where you have a const reference to the parent object. – Gerald Dec 22 '11 at 23:31
  • 1
    @Gerald : Sorry, I should clarify -- what I meant was, if you want the non-const version that returns a reference, then you almost certainly want a const version as well. – ildjarn Jan 10 '12 at 16:29
10

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.

Gerald
  • 23,011
  • 10
  • 73
  • 102
2

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 &).

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
2

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.

thesamet
  • 6,382
  • 2
  • 31
  • 42
1

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.

AJG85
  • 15,849
  • 13
  • 42
  • 50