On Sat, Feb 26, 2011 at 3:27 AM, Nat Linden <nat@lindenlab.com> wrote:

An accessor that returns a const reference to a data member is little better than direct read access to the data member. The reason we use accessors in the first place is to permit us to later introduce additional logic if need be: to return a computed value. But returning a reference forbids that.

Stepping back a little further I'd be inclined to ask why the accessor exists at all. The great get/set debate!
 
I've been burned a few times by a base class with a virtual method returning const std::string&. Of course the base-class implementation simply returns a data member, and it remains fine as long as no subclass needs to override it with real logic. But the first time you have to compute a return value in some override method -- you have to change ALL the return types for that method in the whole class hierarchy. Gack!

Pass by const reference, but return by value.


That would seem to fly in the face of copy elision - see  http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/

- Rob.