On 01/15/11 11:01, Andrew Haines wrote:
> On 01/15/11 10:30, Andrew Haines wrote:
>> On 01/14/11 14:38, Torsten Bonde Christiansen wrote:
>>> Hi List.
>>>
>>> Is it possible to jump a couple of levels in the inherited hierarchy
>>> when calling "inherited" on a method?
>>>
>>>
>>
>> I *think* I've done this before this way but test it to make sure it works:
>>
>> procedure TC.DoSomething:
>> begin
>>  (Self as TA).DoSomething;
>> end;
>>
> 
> Okay I tested that and it didn't work.
> 

For some reason this problem intrigued me so I played around with it and
came up with the attached program that demonstrates a solution that
seems not too hackish to me. (Even though it is one). TC.Dosomething
calls TA.DoSomething and can also have it's own vars and code.

Regards,

Andrew
program Project1;
{$MODE objfpc}{$H+}
{$ASMMODE att}

uses
  Classes, Sysutils;

type

  { TA }

  TA = class
     procedure DoSomething(a,b,c,d,e: Integer); virtual;
  end;

  { TB }

  TB = class(TA)
     procedure DoSomething(a,b,c,d,e: Integer); override;
  end;

  { TC }

  TC = class(TB)
     procedure DoSomething(a,b,c,d,e: Integer); override;
  end;

{ TB }

procedure TB.DoSomething(a,b,c,d,e: Integer);
begin
  inherited DoSomething(a,b,c,d,e);
  WriteLn(Format('TB.DoSomething %d %d %d %d %d',[a,b,c,d,e]));
end;

{ TC }
procedure CallTADoSomething(ATC: TC; aa,bb,cc,dd,ee: integer); assembler; 
nostackframe;
asm
 jmp TA.DoSomething
end;

procedure TC.DoSomething(a,b,c,d,e: Integer);
var
  Buffer: array [0..1024] of byte;
  PossibleEaxResult: Integer;
begin
  FillChar(Buffer, 0, SizeOf(Buffer));
  CallTADoSomething(Self, a,b,c,d,e);
  WriteLn(Format('TC.DoSomething %d %d %d %d %d',[a,b,c,d,e]));
  //now do more stuff
end;

{ TA }

procedure TA.DoSomething(a,b,c,d,e: Integer);
begin
  WriteLn(Format('TA.DoSomething %d %d %d %d %d',[a,b,c,d,e]));
end;


var
 A: TA;
 B: TB;
 C: TC;

begin
  A := TA.Create;
  B := TB.Create;
  C := TC.Create;
  WriteLn('-A-');
  A.DoSomething(1,2,3,4,5);
  WriteLn('-B-');
  B.DoSomething(1,2,3,4,5);
  WriteLn('-C-');
  C.DoSomething(1,2,3,4,5);
  A.Free;
  B.Free;
  C.Free;


end.

_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply via email to