Boost logo

Boost Users :

Subject: Re: [Boost-users] [Filesystem] V3 interface inconsistency
From: Robert Jones (robertgbjones_at_[hidden])
Date: 2011-02-26 15:50:49


On Sat, Feb 26, 2011 at 3:27 AM, Nat Linden <nat_at_[hidden]> 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.



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net