Why "protected" as opposed to "private"?

Voyhkah

Undead
Joined
Apr 25, 2009
Messages
1,444
Location
Earth
I was doing some coding, and a friend asked me why all the classes in CvInfos had their instance varibles under "protected:" instead of "private:". After thinking about it, I realized I didn't have a good answer. So why are they protected instead of private? Do the CvInfos classes have any subclasses? They don't seem to.

Anyone know?
 
Well, the only difference between private and protected is indeed in the access given to subclasses.

Usually, unless you have a good reason to hide these members from subclasses, you put them under protected since you can never know when you want to subclass a class or struct.

Specifically for CvInfos, many of them inherit from CvInfoBase or from CvHotkeyInfo (which inherits from CvInfoBase), and there are other dependencies (especially for art infos), so it is very much conceivable that a specific CvInfo will be subclassed in the future.
 
Also a good approach.
Depending on the system, I guess - whether you have control over everything, how likely is an implementation change, how comfortable is the easy access to the parent class's members and how complicated will it be to change the subclasses should the need arise.

In this case - I think the protected is just more fitting, but I guess my 'usually' from the previous post was a little haste...
 
Back
Top Bottom