Method Pointer



Method Pointer
Smart pascal source code
type TMyProc = procedure; type TClassA = class Field: String; procedure StaticProc; procedure VirtualProc; virtual; end; type TClassB = class(TClassA) class procedure ClassProc; procedure VirtualProc; override; end; procedure TClassA.StaticProc; begin WriteLn(ClassName + ' StaticProc ' + Field); end; procedure TClassA.VirtualProc; begin WriteLn(ClassName + ' VirtualProc ' + Field); end; class procedure TClassB.ClassProc; begin WriteLn(ClassName + ' ClassProc'); end; procedure TClassB.VirtualProc; begin WriteLn(ClassName + ' VirtualProc overriden ' + Field); end; var a := TClassA.Create; var b := TClassB.Create; var p: TMyProc; a.Field := 'Alpha'; b.Field := 'Beta'; p := a.StaticProc; p; p := a.VirtualProc; p; p := b.StaticProc; p; b.ClassProc; p; p := b.VirtualProc; p; p := TClassB.ClassProc; p; a := b; p := a.StaticProc; p; p := a.VirtualProc; p; {<<< RESULT - CONSOLE LOG >>> ----------------------------- TClassA StaticProc Alpha TClassA VirtualProc Alpha TClassB StaticProc Beta TClassB ClassProc TClassB StaticProc Beta TClassB VirtualProc overriden Beta TClassB ClassProc TClassB StaticProc Beta TClassB VirtualProc overriden Beta ----------------------------- {<<<<<<<<< THE END >>>>>>>>>}