object pascal - How to get a pointer to a method in a base class from a child class in Delphi? -


here code example:

type   tmybaseclass = class   public     procedure someproc; virtual;   end;    tmychildclass = class(tmybaseclass)   public     procedure someproc; override;   end;  var   somedelegate: procedure of object;  procedure tmybaseclass.someproc; begin   showmessage('base proc'); end;  procedure tmychildclass.someproc; begin   showmessage('child proc');   // here want pointer tmybaseclass.someproc (not in class!):   somedelegate := someproc; end;  procedure tform1.button1click(sender: tobject); begin   tmychildclass.create   try     // there "child proc" message:     someproc;       free;   end;   // there want "base proc" message, "child proc" again   // (but destroyed anyway, how coud be?):   somedelegate; end; 

the 1 way know is:

procedure tmychildclass.basesomeproc; begin   inherited someproc; end;   procedure tmychildclass.someproc; begin   showmessage('child proc');   somedelegate := basesomeproc; end; 

the 2nd change someproc declaration override reintroduce:

 tmychildclass = class(tmybaseclass)  public     procedure someproc; reintroduce;  end; 

and cast self tmybaseclass (do not use as cast):

 somedelegate := tmybaseclass(self).someproc; 

also note code give access violation because call somedelegate on freed object.


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -