[fpc-pascal] class inheritance and type incompatibility

Sven Barth pascaldragon at googlemail.com
Sat Sep 28 10:37:37 CEST 2013


On 27.09.2013 07:12, Sven Barth wrote:
> Am 27.09.2013 03:52 schrieb "Xiangrong Fang" <xrfang at gmail.com
> <mailto:xrfang at gmail.com>>:
>  > Is there any problem with this solution?
>
> No, at least not on first sight :)

On second sight your solution is not correct, because you are using 
Clone inside your parent class which would not use the (non-virtual) 
Clone you created.

I've now played around with 2.6.2 myself and this should solve this problem:

=== code begin ===

type
   generic TTree<T> = class
   private type
     TSelfType = TTree;
     TSelfClass = class of TSelfType; // earlier I suggested TTree which 
2.6.2 does not support
   public
     function Clone: TTree;
   end;

function TTree.Clone: TTree;
begin
   Result := TSelfClass(Self.ClassType).Create(Data, FParent);
   (...)
end;

=== code end ===

You can of course also add functions like "Clone: TIntTree" to your 
TIntTree which then only call "Result := TIntTree(inherited Clone)", but 
otherwise it should be ok now.

Regards,
Sven



More information about the fpc-pascal mailing list