[fpc-pascal] virtual method and generics

Xiangrong Fang xrfang at gmail.com
Tue Nov 19 10:19:46 CET 2013


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?

Thanks!
Xiangrong
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20131119/173f4ca7/attachment.html>


More information about the fpc-pascal mailing list