[fpc-pascal] casting back a pointer to original type

spir ☣ denis.spir at gmail.com
Sat May 29 15:57:45 CEST 2010


Hello,

Say I store class instances of various types in a TFPList. All happen to be descendants of same superclass C and each one has a specific method "text". When retrieving and writing an element, if they were all direct instances of C, I would do something like:
    element := C(list[index]);   // casting back
    text := element.text;
I cannot do that, even if elements all are *indirect* instances of C, because this calls C.text instead of the element's own proper text method. Which is wrong, indeed.
So, I must cast elements back to their own type; but I have no idea how to do this. Doesn't the compiler store this information? After all, when an element is put into the list, its type is known. Is there a reason why retrieving an element does not cast it back to its original type automatically? Should I store element types manually in a parallel list?
There must be a way to solve this issue, I guess...

Side-question: it seems, when subclassing, that methods with same name and same parameters do not need any modifier keyword (such as "override" or "overload"). Eg I can have
    function text;
in both a superclass C0 and a subclass C1 work as expected. Correct?

Thank you,
Denis
________________________________

vit esse estrany ☣

spir.wikidot.com



More information about the fpc-pascal mailing list