[fpc-pascal] class inheritance and type incompatibility

Xiangrong Fang xrfang at gmail.com
Sun Sep 29 11:47:39 CEST 2013


2013/9/29 Sven Barth <pascaldragon at googlemail.com>

>
> I would suggest you to add an additional protected virtual method to
> TTree<> which is called from TTree<>.Clone and you override that in your
> descendant classes.
>

Could
​ you please give an example how to do this?  I tried the following:

=== code start ===
function TTree.DoClone: TTree; //protected, virtual
begin
  Result := TSelfClass(Self.ClassType).Create(Data, FParent);
end;

function TTree.Clone: TTree;
var
  node: TTree;
begin
  Result := DoClone;
  node := FirstChild;
  while node <> nil do begin
    node.Clone.Remove(Result);
    node := node.NextSibling;
  end;
end;
=== code end ===

However, this does not work, because it seems that I cannot make any
generic method virtual!  In TIntTree, I have to write:

function TIntTree.DoClone: TIntTree;

As it is not possible to write TTree outside of the generic definition.

Regards,
Xiangrong
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20130929/6ac6a9dd/attachment.html>


More information about the fpc-pascal mailing list