It returns a link to a private member.
There are many cases where this is desirable, but care must be taken.
An IMO is generally not recommended to return a copy of an internal object that is not an integral type for general performance reasons. Yes, I know, premature optimization is not very good, but actually it is not optimization, it is just good performance practice that allows the caller to determine the consequences of performance; if he wants a copy, he may simply not declare the variable that he assigns to it as a reference.
There are two general rules that I use here:
1) If you do not want the caller to be able to directly modify the private object, declare the return value as a reference to a constant:
inline const string& GetLabel() const{ return m_Label; }
2) The caller should not store the link returned by the class method, it should be used only locally, where the parent is guaranteed to be in scope.
If for some reason you need callers to be able to store a link to your internal objects, use smart pointers instead.
Gerald
source share