[Exceptional C++ Style] Item 17 - Encapsulation

Timothy Wright tcw321 at gmail.com
Mon Dec 20 15:34:11 EST 2004


> 
> To bring this back to the original point - should classes be allowed expose
> member data if that data is const? I still say no. The function of the class
> should be made apparent via its interface, and even if all a class did was
> provide access to some const data, it is still advantageous to wrap this in
> an interface. If there was just a single reason to do this, surely that is
> all it should take to convince you? 

I concur.  I am always changing my code refactoring due to changing
needs.  I am never sure of anything.  Code I write this week might
need to be changed next week.  If I make members public even if they
are const, it ties my hands and requires more changes to more code if
and when they come.  If the values are accessed thru get/set I have
least one indirection that could help me.

I have a hard time knowing if my design decisions today might haunt me
tomorrow (and usually they do) so I take as much insurance as possible
with encapsulation.  At least more now that I have done in the past.

Regards

Tim



More information about the Effective-cpp mailing list