General inheritance/virtual method skipping parent class execution method of grandparent class

Source: Internet
Author: User

Background: One day accidentally wrote the base class function virtual function virtual, when the subclass override overrides, there are colleagues need to skip the parent class to execute the method code of the grandfather class;

Found T grandfather Class (self). * * Method (), exception. Because the virtual function is transformed into a grandparent, it is a polymorphic form, or it executes the method of the book class, all resulting in a cyclic execution of code, resulting in a stack overflow;

Online Baidu to Acer hack function to find the Grandfather class method base site execution.

Solution One: No virtual method, the general inheritance can be.

UnitUnit1;InterfaceusesWindows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls;typeTmyclassa=class(tstringlist) Public    procedureKKK ();//virtual;  End; TMYCLASSB=class(Tmyclassa) Public    procedureKKK ();//Override;  End; TMYCLASSC=class(TMYCLASSB) Public    procedureKKK ();//Override;  End; TForm1=class(tform) Button1:tbutton;    Button2:tbutton;    Button3:tbutton; procedureformcreate (Sender:tobject); procedureButton1Click (Sender:tobject); procedureButton2click (Sender:tobject); procedureButton3click (Sender:tobject); Private    {Private Declarations}A:tmyclassa;    B:TMYCLASSB;  C:TMYCLASSC;  Public    {Public Declarations}  End;varForm1:tform1;Implementation{$R *.DFM}proceduretmyclassa.kkk ();beginShowMessage ('TMYCLASSA.KKK');End;proceduretmyclassb.kkk ();begin  inherited; ShowMessage ('TMYCLASSB.KKK');End;proceduretmyclassc.kkk ();begin  //inherited; Tmyclassb.kkk () is not executed after comment //Tmyclassa (self). KKK;ShowMessage ('TMYCLASSC.KKK');End;proceduretform1.formcreate (sender:tobject);beginA:=tmyclassa.Create ; B:=tmyclassb.Create; C:=TMYCLASSC.Create;End;procedureTform1.button1click (sender:tobject);begina.kkk;End;procedureTform1.button2click (sender:tobject);beginb.kkk;End;procedureTform1.button3click (sender:tobject);beginc.kkk;End;End.

Workaround Two: The virtual method skips the parent class that inherits the code that calls the grandparent class http://blog.csdn.net/coolbaby/article/details/485652

procedureTcurrentobject.avirtualmethod;begin  ASMMOV edx,vmtoffset Avirtualmethod//virtual method VMT offsetMOV eax,self//object instance = EAXMOV Ecx,[eax]//class VMT = ECXmov ecx,[ecx].vmtparent mov ecx,[ecx]//Parent Class VMT = ECXmov ecx,[ecx].vmtparent mov ecx,[ecx]//Grandfather Class vmt = ECXMOV Ecx,[ecx+edx]//Grandfather class virtual method address = = ECXCall ECX//Method Invocation  End;End;
UnitUnit1;InterfaceusesWindows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls;typeTmyclassa=class(tstringlist) Public    procedureKKK ();Virtual; End; TMYCLASSB=class(Tmyclassa) Public    procedureKKK ();Override; End; TMYCLASSC=class(TMYCLASSB) Public    procedureKKK ();Override; End; TForm1=class(tform) Button1:tbutton;    Button2:tbutton;    Button3:tbutton; procedureformcreate (Sender:tobject); procedureButton1Click (Sender:tobject); procedureButton2click (Sender:tobject); procedureButton3click (Sender:tobject); Private    {Private Declarations}A:tmyclassa;    B:TMYCLASSB;  C:TMYCLASSC;  Public    {Public Declarations}  End;varForm1:tform1;Implementation{$R *.DFM}proceduretmyclassa.kkk ();beginShowMessage ('TMYCLASSA.KKK');End;proceduretmyclassb.kkk ();begin  inherited; ShowMessage ('TMYCLASSB.KKK');End;proceduretmyclassc.kkk ();begin  //inherited; Tmyclassb.kkk () is not executed after comment //Tmyclassa (self).    KKK; The virtual method will be abnormal, loop called,  ASMMOV edx,vmtoffset KKK//virtual method VMT offsetMOV eax,self//object instance = EAXMOV Ecx,[eax]//class VMT = ECXmov ecx,[ecx].vmtparent mov ecx,[ecx]//Parent Class VMT = ECXmov ecx,[ecx].vmtparent mov ecx,[ecx]//Grandfather Class vmt = ECXMOV Ecx,[ecx+edx]//Grandfather class virtual method address = = ECXCall ECX//Method Invocation  End; ShowMessage ('TMYCLASSC.KKK');End;proceduretform1.formcreate (sender:tobject);beginA:=tmyclassa.Create ; B:=tmyclassb.Create; C:=TMYCLASSC.Create;End;procedureTform1.button1click (sender:tobject);begina.kkk;End;procedureTform1.button2click (sender:tobject);beginb.kkk;End;procedureTform1.button3click (sender:tobject);beginc.kkk;End;End.
View Code

General inheritance/virtual method skipping parent class execution method of grandparent class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.