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

Andrew Hall andrew.hall at shaw.ca
Sun May 30 01:54:44 CEST 2010


On 29 May 10, at 15:26 , spir ☣ wrote:
> I cannot do that. C0 (and all classes) instances need a text method. I also cannot have 2 methods (one static, one virtual) with different names. It's a basic feature, always called with the same name. Like getItem for a hierarchy of collections: every collection must have it, and always under the same name, so that any client can rely on it.

David is not suggesting you have two methods with different names - his example is demonstrating the different behaviour of static and virtual methods. Calling a static method is determined at compile time - the compiler must use the actual class of your variable (or cast) - so your descendent methods will never be called.  This is what is happening to you at the moment - only your base class Text method is called.  Calling a virtual method is determined at runtime based upon the actual class held by the variable (cast) - it seems this is exactly what you need.  Set your base class Text method as virtual, and each descendent class Text method as override - your code should then function as you require...

   element := C(list[index]);   // casting back
   text := element.text;

if the pointer in list[index] is C1, C1.Text will be called.

Regards,  
Andrew.




More information about the fpc-pascal mailing list