Am 19.11.2013 10:21 schrieb "Xiangrong Fang" <xrf...@gmail.com>: > > Hi, > > In my TTree class: https://github.com/xrfang/fpcollection/blob/master/src/units/tree.pas > > I have the following method: > > function TTree.Load(s: TStream): Integer; > var > lv, c: QWord; > node: TTree; > buf: Pointer; > begin > Clear; > if not ReadNodeData(s, lv, buf, c) then Exit(0); > DoRestore(buf); > OnRestore; //<-- CALL#1 > FreeMem(buf, c); > Result := 1; > node := Self; > while ReadNodeData(s, lv, buf, c) do begin > while (node <> nil) and (lv < node.Level) do node := node.Parent; > if (lv = node.Level) and (node <> Self) then > node := TTree.Create(Data, node.Parent) > else > node := TTree.Create(Data, node); > node.DoRestore(buf); > node.OnRestore; //<-- CALL#2 > FreeMem(buf, c); > Inc(Result); > end; > end; > > 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. > > How to solve this problem?
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. Also this problem is not related to generics, but would happen in a non-generic class as well. Regards, Sven
_______________________________________________ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal