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