<p>Am 19.11.2013 10:21 schrieb "Xiangrong Fang" <<a href="mailto:xrfang@gmail.com">xrfang@gmail.com</a>>:<br>
><br>
> Hi,<br>
><br>
> In my TTree class: <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>
> I have the following method:<br>
><br>
> function TTree.Load(s: TStream): Integer;<br>
> var<br>
> lv, c: QWord;<br>
> node: TTree;<br>
> buf: Pointer;<br>
> begin<br>
> Clear;<br>
> if not ReadNodeData(s, lv, buf, c) then Exit(0);<br>
> DoRestore(buf);<br>
> OnRestore; //<-- CALL#1<br>
> FreeMem(buf, c);<br>
> Result := 1;<br>
> node := Self;<br>
> while ReadNodeData(s, lv, buf, c) do begin<br>
> while (node <> nil) and (lv < node.Level) do node := node.Parent;<br>
> if (lv = node.Level) and (node <> Self) then<br>
> node := TTree.Create(Data, node.Parent)<br>
> else<br>
> node := TTree.Create(Data, node);<br>
> node.DoRestore(buf);<br>
> node.OnRestore; //<-- CALL#2<br>
> FreeMem(buf, c);<br>
> Inc(Result);<br>
> end;<br>
> end; <br>
><br>
> Now I have a problem. CALL#1 is virtual as expected (sub-class's OnRestore is called), but CALL#2 is not virtual, it just call the (empty) OnRestore method defined in TTree.<br>
><br>
> How to solve this problem?</p>
<p>The problem is that you are creating a TTree instance (the TTree.Create) and not an instance of the current node type. Use "node := TTree(ClassType).Create(...)" instead of "node := TTree.Create(...)" at the two locations where you create the node.</p>
<p>Also this problem is not related to generics, but would happen in a non-generic class as well.</p>
<p>Regards,<br>
Sven</p>