[fpc-pascal] class inheritance and type incompatibility

Xiangrong Fang xrfang at gmail.com
Thu Sep 26 08:39:29 CEST 2013


Hi All,

I have the following program:

  1 program test;
  2 {$mode objfpc}{$H+}
  3 uses tree;
  4 type
  5   TIntTree = class(specialize TTree<Integer>)
  6   public
  7     function Clone: TIntTree;
  8   end;
  9 function TIntTree.Clone: TIntTree;
 10 begin
 11   Result := TIntTree(inherited Clone);
 12 end;
 13 var
 14   it1, it2 : TIntTree;
 15 begin
 16   it1 := TIntTree.Create(1, nil);
 17   it2 := it1.Clone;
 18   WriteLn(it1.ClassName);
 19   WriteLn(it2.ClassName);
 20 end.

Which output:

TIntTree
TIntTree.TTree$LongInt

The source code of TTree is here:
https://github.com/xrfang/fpcollection/blob/master/src/units/tree.pas

Why the typecast on line 11 does not work? How to modify the code so that
the cloned object has same type as the original one?

Regards,
Xiangrong
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20130926/bd6ff852/attachment.html>


More information about the fpc-pascal mailing list