Re: Override public virtual Functions with private Functions?
On May 26, 2:56 pm, Chris Gordon-Smith <use.addr...@my.homepage>
wrote:
I have a base class called Action_Request, and a set of
classes corresponding to different kinds of Action_Request,
each of which inherits from Action_Request. Eg:-
class Add_Molecule_Req: public Action_Request{
// ......
};
I manipulate the various derived classes polymorphically
through Action_Requests's public interface, using its virtual
functions. Currently the overriding functions in the derived
classes (including the destructor, which overrides
Action_Request's virtual destructor), are declared public.
My program compiles if I make them private, and doing this
would seem to have the advantage that these functions must
then always be invoked polymorphically through
Action_Request's public interface, which is what I want.
My questions are:-
i) Is overriding public virtual functions with private functions good
practice?
ii) Are there any disadvantages?
It's more a style issue than anything else. My personal
guidelines are to never change the access of a virtual function
in the derived class, since I find it confusing that the same
function has different access depending on the static type used
to access it. The disadvantage that I see is that someone
working on the derived class, later, may think that the
functions in question cannot be called from outside the class.
And of course, making them private does sort of violate the LSP:
if I have a Derived, I can't call them, although I could if I
had a Base.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34