<p>Am 23.09.2013 09:10 schrieb "Xiangrong Fang" <<a href="mailto:xrfang@gmail.com">xrfang@gmail.com</a>>:<br>
><br>
> Hi All,<br>
><br>
> I wrote a TTree generic class, the code is here:<br>
><br>
> <a href="https://github.com/xrfang/fpcollection/blob/master/src/units/tree.pas">https://github.com/xrfang/fpcollection/blob/master/src/units/tree.pas</a><br>
><br>
> While using this class, I encountered the following problem:<br>
><br>
> program project1;<br>
> {$mode objfpc}{$H+}<br>
> uses tree;<br>
> type<br>
>   TIntTree = class(specialize TTree<Integer>)<br>
>   end;<br>
> var<br>
>   ti : TIntTree;<br>
> begin<br>
>   ti := TIntTree.Create(1, nil);<br>
>   ti := ti.Next;  <-- error here<br>
> end.<br>
><br>
> The error message is:<br>
><br>
> Error: Incompatible types: got "TIntTree.TTree$LongInt" expected "TIntTree"<br>
><br>
> If I do not inherit TTree but just specialize it, there is no error.<br>
><br>
> How can I solve this problem so that I don' t have to use typecast everywhere?</p>
<p>Short answer: you can't. The same would happen with normal classes.</p>
<p>Long answer: You could add an additional method "Next" to your "TIntTree" which returns a "TIntTree" and just do "Result := TIntTree(inherited Next);" there.</p>
<p>Why are you subclassing in this case anyway?</p>
<p>Regards,<br>
Sven</p>