14

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!

5 Answers 5

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.

3
  • Indeed, you probably want both the const and non-const versions.
    – ildjarn
    Commented Dec 22, 2011 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
    Commented Dec 22, 2011 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
    Commented Jan 10, 2012 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.

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

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.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.