ComponentBoundsConstrainer question


#1

I’m creating an inheritor from ComponentBoundsConstrainer, and trying to override checkBounds(), but hitting a problem. Normally I can copy and paste the body of the function I’m overriding into my class, and then alter it, but I can’t do that in this case.

For one, all the minima and maxima for height and width are private. Not a big deal, I can use the accessor functions for those as replacements.

checkBounds() also uses minOffTop and related private member variables, and I can’t access these in my public inheritor, as there are no accessor functions.

Is there a way around this? In my own classes I tend to make member variables protected rather than private, so that inheritors can access member variables directly, without having to use accessors. Is that bad design?


#2

Can’t you just call ComponentBoundsConstrainer::checkBounds() at the start of your overloaded method? Are you copying and pasting because you didn’t know about being able to do that in c++, or because of some other reason?


#3

In the end I did do that. I’m still curious about the more general question, however: are there advantages to having member variables be private rather than protected in classes that you expect will be overridden.

I can see pros and cons to both philosophies, so getting your perspective might help me improve my code down the road.


#4

I don’t really have a philosophy on it that I could explain, it’s more just a feeling for which bits of a class I don’t want people to mess with!

The main reason it’s best to keep things private is that if someone’s subclass expects a variable to behave in a certain way, and then you change the way the base class is implemented, it could silently break all the derived code.


#5