Overall, I don't see much point in using private members. |
Encapsulation.
A properly encapsulated class is impossible (or at least extremely difficult) to misuse.
Take std::string for example. It has a member to record the length, and a member which is a pointer to the string buffer, but you can't access them directly. If the user (you) had access to those members, it would be really easy to screw things up and cause buffer overflows, memory corruption, and all sorts of other nasty problems.
But because those members are private, you
can't misuse std::string. It'll always* work exactly right.
*(note that it's always possible to screw things up.... like with memcpy or with other kinds of memory corruption)
However they are quite useless otherwise and only make things more complicated. |
This is the opposite of the truth.
If you write a class and it's properly encapsulated, then [after testing] you can rest easy knowing that your class "just works". Which means any code in your program that uses the class will "just work".
If you have public members everywhere and any part of your program can mess with any member of any object... what happens when bugs start popping up?
If the bug caused by the code using the class?
Or is the bug in the class itself?
How can you track them down?
Not everybody gets OOP. I didn't for a long time... until one day it just clicked.
EDIT:
As for the original question:
I suppose it depends on the situation and how complicated the class is, but I'd say just access the private members directly.
I was never a fan of pointless get/set functions that do nothing but assign and return a private variable. Such functions make even less sense when they're private.