On 27.09.2013 07:12, Sven Barth wrote:
Am 27.09.2013 03:52 schrieb "Xiangrong Fang" <xrf...@gmail.com
<mailto:xrf...@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
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply via email to