[Exceptional C++ Style] Item 17 - Encapsulation

Adrian Fagg adrianf at evaluategroup.com
Tue Dec 21 05:40:21 EST 2004


Ooops, I replied directly to Balog Pal, not the list due to a mistake in my
email configuration.

> -----Original Message-----
> From: effective-cpp-bounces at accu.org
> [mailto:effective-cpp-bounces at accu.org]On Behalf Of Balog Pal
> Sent: 20 December 2004 20:49
> To: effective-cpp at accu.org
> Subject: Re: [Exceptional C++ Style] Item 17 - Encapsulation
>
>
> [repost]
> > So you could write:
> >
> > const SomeData& Case1::GetSomeData()const
> > {
> >     return SomeData(myInternalRep);
> > }
>
> Yeah that is the problem: you can't return a temporary/rvalue
> by reference.
> To do that kind of stuff the getter should return by value.

I will say one thing in defence of the original:
It is possible to change this particular interface to return by value
without making any syntactic or semantic changes to most client code. i.e.
assuming the client code wasn't hanging on to a const reference beyond the
immediate scope, by e.g. initialising another object containing such a
reference. OTOH, by implementing a return by reference in the first place
(assuming the object is expensive to copy), you have a reasonable chance of
getting the efficiency of direct data member access.

However, anyone who knows me will know that my real bugbear is the whole
idea of 'getter' and 'setter' functions in the first place. An aspect of
this which takes us straight back to the point about trying to get
interfaces right first time...

Regards,

Adrian

Work: mailto:adrianf at evaluategroup.com
Home: mailto:adrian.fagg at ntlworld.com

Pretty pictures: http://homepage.ntlworld.com/rbyadf/




More information about the Effective-cpp mailing list