According to https://en.cppreference.com/w/cpp/language/virtual#In_detail overriding a base's virtual
member function only care about the function name, parameters, const/volatile-ness and ref qualifier. It doesn't care about return type, access modifier or other things you might expect it to care about.
The linked reference also specifically notes that :
Base::vf does not need to be visible (can be declared private, or inherited using private inheritance) to be overridden.
Nothing that I can find explicitly gives permission to do this, but the rules of overriding do not prevent it. It's allowed by virtue of virtual
functions and function overriding existing and not disallowing this case.
If you are asking why this is how the language is, you may have to ask the standardization committee.
virtual
andoverride
simply do not pay any attention to access modifiers. Notably, it's not part of the function signature, which is whatvirtual
andoverride
look at. – KimonBase& base = a; base.do_run();
would produce error. – Beamendsrun
that makes sure that it can't be run twice. In my specific implementation, that logic isn't required because it's done indo_run
(because of some hardware feature - who knows), which makes them identical; I can safely calldo_run
orrun
; but I know thatdo_run
will be just that little bit faster because it doesn't need to go through into the checking ofrun()
. Well, why shouldn't I let things that KNOW they're using this implementation use the faster version; while still allowing things that don't to still work? – Melodramatize