[fpc-pascal] class inheritance and type incompatibility
Xiangrong Fang
xrfang at gmail.com
Sat Sep 28 16:37:06 CEST 2013
2013/9/28 Sven Barth <pascaldragon at googlemail.com>
>
> On second sight your solution is not correct, because you are using Clone
> inside your parent class which would not use the (non-virtual) Clone you
> created.
>
What If I make Clone virtual? If by adding "virtual" to Clone, the problem
is solved, then what's the difference between virtual Clone and the method
you provided?
> === code begin ===
>
> type
> generic TTree<T> = class
> private type
> TSelfType = TTree;
> TSelfClass = class of TSelfType; // earlier I suggested TTree which
> 2.6.2 does not support
This compiles, however, if I remove my version of TIntTree (i.e. implement
a Clone in it and call parent's DoClone), then the main program does not
compile, it says:
demo2.lpr(20,13) Error: Incompatible types: got "TIntTree.TTree$LongInt"
expected "TIntTree"
I then can either:
1) add a Clone method to TIntTree and do a typecast inside it, as you said.
2) do a typecast in the main program, i.e. it2 := it1.Clone as TIntTree.
So, I guess the problem is solved? I have two further questions:
1) Why class of TTree does not work in 2.6.2 but class of TSelfType worked?
Is it a problem of generics in 2.6.2, or it is *not* related to generics?
I feel that TSelfType is same as TTree?
2) What is the difference between the following:
- it2 := TIntTree(it1.Clone);
- it2 := it1.Clone as TIntTree;
Where in the official document can I find the behavior/definition of "as"?
Thanks a lot.
Regards,
Xiangrong
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20130928/c85a2a79/attachment.html>
More information about the fpc-pascal
mailing list