delphi - "Method '%s' hides virtual method of base type '%s'". What's really being hidden? -


after having read ian boyd's constructor series questions (1, 2, 3, 4), realize don't quite grasp literal meaning on what's being hidden.

i know (correct me if i'm wrong) override's sole purpose able have polymorphic behavior, run-time can resolve method depending on actual type of instance - opposed declared type. consider following code:

type   tbase = class     procedure proc1; virtual;     procedure proc2; virtual;   end;    tchild = class(tbase)     procedure proc1; override;     procedure proc2;            // <- [dcc warning]   end;  procedure tbase.proc1; begin   writeln('base.proc1'); end; procedure tbase.proc2; begin   writeln('base.proc2'); end;  procedure tchild.proc1; begin   inherited proc1;   writeln('child.proc1'); end; procedure tchild.proc2; begin   inherited proc2;   writeln('child.proc2'); end;  var   base: tbase; begin   base := tchild.create;   base.proc1;   writeln;   base.proc2;   base.free;   readln; end. 

which outputs:

base.proc1
child.proc1

base.proc2

the warning on tchild.proc2 states that method "will hide access base's method of same name". see is, if don't override proc2 loose ability of method's resolving actual type, not of base type. how's hiding access base's method?

further, down documentation on warning solution warning, stated that:

first, specify override make derived class' procedure virtual, , allowing inherited calls still reference original procedure.

now, if create 'tchild' instance 'tchild' (no polymorphism), inherited call in non-overridden method refers original procedure. if create 'child' instance 'tbase', call not resolve 'tchild' method, how call 'inherited' refer @ all?

what misunderstanding?

amongs other thing, won't able define

tgrandchild = class(tchild)    procedure proc2; override; end;  

because proc2 tgrandchild sees 1 tchild not virtual. tchild.proc2 hide tbase.proc2 descendants.

edit:

in answer sertac's comment:

var    base: tbase;    child : tchild begin    child := tchild.create;   base := child;   base.proc2;    child.proc2;    base.free;    readln;  

that output

base.proc2 base.proc2 child.proc2 

so, seems call same method twice call 2 different methods. makes code harder understand (which not practical) , yield unexpected behavior.


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -