Quote:Original post by Kest
Don't get me wrong. I completely understand the reasons to use Get/Set and private members. But I'm saying there are times when I know the data is safe to do anything with, such as a generic bool value. How badly screwed can a program become because of the value of a bool member?
Encapsulation isnt just to enforce variants.
The client code which uses the classes that you write should be unable to use your class in the wrong way. 'What' your class does should be strongly seperated from 'how' it will do it. If you are fiddling with a classes data you are working with the 'how', not with the 'what'.
Look at the STL - 'what' it does is clearly defined standard, but every different C++ compiler has a different implementation of how it will do it. Your classes should be the same; if you decide to change the implementation, it should have no effect on the clients. If the clients have their fingers in your private data though, then changing the implementation will break the client code.
If you've decided in the design of a class that there is a boolean that should be able to be changed at any time to indicate a certain state, you could make it public, but then if at a later stage you decide you want to pack those 30 booleans into a single integer, you have to go and update every bit of code that uses the boolean. Whereas if you used a function, like:
void SetMyState( bool b ) { m_bMyState = b; }
Then it can easily be changed to:
void SetMyState( bool b ) { b ? m_iFlags |= MY_STATE : m_iFlags &= ~MY_STATE; }
Anyone who writes professional C++ code MUST read Scott Meyers "Effective C++"