[fpc-pascal] Skipping an the "inherited" of an ancestor

Martin fpc at mfriebe.de
Sat Jan 15 15:49:45 CET 2011


On 15/01/2011 11:09, Torsten Bonde Christiansen wrote:
>>
>>     Is it possible to jump a couple of levels in the inherited
>>     hierarchy when calling "inherited" on a method?
>>
>>
>> Hmm, don't know whether you're the same person or not :), but I 
>> replied in a stackoverflow question the next day after it was asked 
>> (see here: 
>> http://stackoverflow.com/questions/4662744/delphi-how-to-call-inherited-inherited-ancestor/4670457#4670457 
>> ) and it looked like the host didn't notice :). At least this variant 
>> seemed to work
>
> I'm sorry to say that it is not I who wrote on stackoverflow, but 
> could as well have been. Anyways, your solution does not really solve 
> the problem, since i will have to introduce a HackedParent class and 
> that kinds of defeat the purpose.
> All I really want is to skip to a grandparent class using the normal 
> "inherited" functionality. I have tried to look through the 
> mailinglist archive, because I seem to remember someone else might 
> have asked this question as well - but I haven't been able to find 
> anything (yet).
Here is another "solution" (another hack):


program Project1;
{$mode objfpc}{$H+}

type

   { TFoo }

   TFoo = class
   protected
     procedure DoHello; virtual;
   end;

   { TFooChild }

   TFooChild = class(TFoo)
   protected
   end;

   { TFooGrandChild }

   TFooGrandChild = class(TFooChild)
   protected
     procedure DoHello; override; // to be skipped
   end;

   { TFooGreatGrandChild }

   TFooGreatGrandChild = class(TFooGrandChild)
   protected
     procedure DoHello; override;
   end;

{ TFoo }

procedure TFoo.DoHello;
begin
   writeln('TFoo');
end;

{ TFooGrandChild }

procedure TFooGrandChild.DoHello;
begin
   writeln('TFooGrandChild');
   inherited DoHello;
end;

{ TFooGreatGrandChild }

procedure TFooGreatGrandChild.DoHello;
var
   a: procedure of object;
begin
   writeln('TFooGreatGrandChild');
   //inherited DoHello;
   TMethod(a).Code := @TFooChild.DoHello;
   TMethod(a).Data:= Self;
   a();
end;

var
   a: TFoo;
begin
   a := TFooGreatGrandChild.Create;
   a.DoHello;
   readln;
end.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20110115/4adde259/attachment.html>


More information about the fpc-pascal mailing list