I understand that when a class is private only the class is allowed to access the members. |
Back to private classes.
Public inheritace implements "has a" relationship. Private inheritance implements "implemented in terms of" relationship.
The effect of private inheritance is to allow the derived class to overide virtual methods, but the details (methods and data) of the base class are invisible to the user of the derived class.
There are a number of
features that follow specific implementation patterns in C++, this is one of them.
Concerning my comment:
Class data should never be public or protected, it should be private.
Hands up all those who think that MFC's CWnd class should have a public data member m_hWnd.
How more costly would it be if MFC's CWnd class had an inlined public member method GetHandle() that returned the window handle?
How much more flexible would it be if access to an MFC window handle were accessed through a method? Maybe a debug version could assert the validity of the handle value or a checked build assert the validity of the handle itself? You can't do any of that because someone thought it would be more efficient to make the data public 16 years ago.
That is entirely dependent on the problem being solved. |
On noddy little apps it doesn't matter what you do.
I hate devs who make everything private and cause deriving to be a pain... |
I hate shortcuts, they always come back to haunt you.
I'm not going to go and ask the car what color it is, I'll just look at it, and see for myself...there are definitely some cases in which even public data is acceptable. |
You can only talk to an object by sending it a message--those are the rules in OO. C++ makes OO efficient by checking senders/receivers at runtime so it can perform the sending the message as efficiently as in C. Of course, not all C++ programs are object oriented, my statement doesn't apply to other domains.