<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">2013/9/29 Sven Barth <span dir="ltr"><<a href="mailto:pascaldragon@googlemail.com" target="_blank">pascaldragon@googlemail.com</a>></span><br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
If you want to override the virtual Clone method of TTree<LongInt> in TIntTree you need to use the same result type + override or otherwise you gain nothing, because the compiler will not consider the method as overloaded and use the Clone method of TTree<LongInt> inside TTree<LongInt>'s methods instead.</blockquote>
<div><br><div class="gmail_default" style="font-family:courier new,monospace;display:inline">Yes, you are right, for virtual method, you need have exactly same type... But this leads me to think how the generics are implemented? For example:<br>
<br></div><div class="gmail_default" style="font-family:courier new,monospace;display:inline">=== snippet 1 ===<br></div><div class="gmail_default" style="font-family:courier new,monospace;display:inline">type<br></div><div class="gmail_default" style="font-family:courier new,monospace;display:inline">
TIntTree = class(specialize TTree<Integer>)<br></div><div class="gmail_default" style="font-family:courier new,monospace;display:inline"> public<br></div><div class="gmail_default" style="font-family:courier new,monospace;display:inline">
function Clone: TTree; override;<br></div><div class="gmail_default" style="font-family:courier new,monospace;display:inline"> end;<br></div><div class="gmail_default" style="font-family:courier new,monospace;display:inline">
=== end of snippet 1 ===<br><br></div><div class="gmail_default" style="font-family:courier new,monospace;display:inline">This will leads to a compile error: Generics without specialization cannot be used as a type for a variable.<br>
<br></div><div class="gmail_default" style="font-family:courier new,monospace;display:inline">If so, why in the code TTree can be used everywhere, for example:<br><br></div><div class="gmail_default" style="font-family:courier new,monospace;display:inline">
=== snippet 2 ===<br></div><div class="gmail_default" style="font-family:courier new,monospace;display:inline">function TTree.Level: Cardinal;<br>var<br> n: TTree; <-- here TTree is not specialized.<br>begin<br> ... ...<br>
end;<br></div><div class="gmail_default" style="font-family:courier new,monospace;display:inline">=== end of snippet 2 ===<br><br></div><div class="gmail_default" style="font-family:courier new,monospace;display:inline">
In snippet 1, why the compiler don't treat the TTree same as its own type i.e. TIntTree? I imagine that TTree in snippet 1 is logically similar to TObject. I mean, a class's method is of course possible to return a value of its parent's type or any other class's instance?<br>
<br></div></div><div></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class="im"></div>
Would you please show how exactly you changed the code? With all those methods calling each other it's a bit hard to imagine in my head what you changed. ;)<br></blockquote></div><br><div class="gmail_default" style="font-family:courier new,monospace">
With your TSelfClass solution in TTree, I mean either:<br><br></div><div class="gmail_default" style="font-family:courier new,monospace">=== solution 1 ===<br></div><div class="gmail_default" style="font-family:courier new,monospace">
type<br></div><div class="gmail_default" style="font-family:courier new,monospace"> TIntTree = class(specialize TTree<Integer><br></div><div class="gmail_default" style="font-family:courier new,monospace"> public<br>
</div><div class="gmail_default" style="font-family:courier new,monospace"> function Clone: TIntTree;<br></div><div class="gmail_default" style="font-family:courier new,monospace"> end;<br></div><div class="gmail_default" style="font-family:courier new,monospace">
function TIntTree.Clone: TIntTree;<br></div><div class="gmail_default" style="font-family:courier new,monospace"> begin<br></div><div class="gmail_default" style="font-family:courier new,monospace"> Result := TIntTree(inherited Clone); //typecast in TIntTree<br>
</div><div class="gmail_default" style="font-family:courier new,monospace"> end;<br></div><div class="gmail_default" style="font-family:courier new,monospace">=== end of solution 1===<br></div><div class="gmail_default" style="font-family:courier new,monospace">
or:<br></div><div class="gmail_default" style="font-family:courier new,monospace">=== solution 2 ===<br></div><div class="gmail_default" style="font-family:courier new,monospace">type<br></div><div class="gmail_default" style="font-family:courier new,monospace">
TIntTree = class(specialize TTree<Integer>)<br></div><div class="gmail_default" style="font-family:courier new,monospace"> end;<br></div><div class="gmail_default" style="font-family:courier new,monospace"></div>
<div class="gmail_default" style="font-family:courier new,monospace">begin //main <br></div><div class="gmail_default" style="font-family:courier new,monospace"> it2 := TIntTree(it1.Clone); //typecast in main program<br>
</div><div class="gmail_default" style="font-family:courier new,monospace">end.<br></div><div class="gmail_default" style="font-family:courier new,monospace">=== end of solution 2 ===</div><div class="gmail_default" style="font-family:courier new,monospace;display:inline">
<br></div><div class="gmail_default" style="font-family:courier new,monospace;display:inline">Are both solutions correct?<br><br></div><div class="gmail_default" style="font-family:courier new,monospace;display:inline">
Regards,<br>
Xiangrong</div><br></div></div>