[Exceptional C++ Style] Item 17 - Encapsulation

Adrian Fagg adrianf at evaluategroup.com
Mon Dec 20 10:43:55 EST 2004


> -----Original Message-----
> From: effective-cpp-bounces at accu.org
> [mailto:effective-cpp-bounces at accu.org]On Behalf Of solosnake .
> Sent: 20 December 2004 15:02

> > > const SomeData& Case1::GetSomeData()const
> > > {
> > >     return someData_;
> > > }
> >
> >Hey, hold on - that's giving you a pretty strong dependency on its 
> >internals
> >as well isn't it?
> >
> >Regards,
> >
> >Adrian
> 
> The dependancy is purely on the class / implementation side - 
>  there is *no* 
> dependancy for the end user of my class - this is the 
> important difference.  
> As an example, lets imagine you and I are co-operating on a  
> project - I am 
> writing the class that provides you with the 'SomeData' you 
> need. If I 
> provide you with access to it via an accessor function, then 
> your code 
> doesnt need any changes when at a later date I need to make 
> changes to the 
> internals of my class. With this in mind, is the differance 
> clear? Perhaps 
> we are focusing too much on authoring the class code, and 
> ignoring the user 
> of the class?

So you could write:

const SomeData& Case1::GetSomeData()const
{
     return SomeData(myInternalRep);
}

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