[fpc-pascal] class inheritance and type incompatibility
    Xiangrong Fang 
    xrfang at gmail.com
       
    Fri Sep 27 03:50:53 CEST 2013
    
    
  
2013/9/26 Sven Barth <pascaldragon at googlemail.com>
>
> After change to this code in Clone(), I got this error:
>>
>> 
>> tree.pas(66,60) Error: Incompatible type for arg no. 2: Got
>> "TIntTree.TTree$LongInt", expected "TTree"
>>
>
> Seems like a bug in 2.6.x. It's compiling in 2.7.1.
>
I altered the code so that it works in 2.6.2:
=== test program ===
program test;
{$mode objfpc}{$H+}
uses tree;
type
  TIntTree = class(specialize TTree<Integer>)
  public
    function Clone: TIntTree;
  end;
function TIntTree.Clone: TIntTree;
begin
  Result := TIntTree.Create(Data, FParent);
  DoClone(Result);
end;
var
  it1, it2 : TIntTree;
begin
  it1 := TIntTree.Create(1, nil);
  it2 := it1.Clone;
  WriteLn(it1.ClassName);
  WriteLn(it2.ClassName);
end.
=== tree clone code ===
type
  generic TTree<T> = class
    ... ...
  protected
    procedure DoClone(Target: TTree);
  public
    ... ...
    function Clone: TTree;
    ... ...
  end;
implementation
procedure TTree.DoClone(Target: TTree);
var
  node: TTree;
begin
  node := FirstChild;
  while node <> nil do begin
    node.Clone.Remove(Target);
    node := node.NextSibling;
  end;
end;
function TTree.Clone: TTree;
begin
  Result := TTree.Create(Data, FParent);
  DoClone(Result);
end;
=======
Is there any problem with this solution?
Regards,
Xiangrong
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20130927/d21aa85d/attachment.html>
    
    
More information about the fpc-pascal
mailing list