[fpc-pascal] Cast, inheritance, etc

Sven Barth pascaldragon at googlemail.com
Tue Dec 4 20:53:13 CET 2012


On 04.12.2012 20:03, Marcos Douglas wrote:
> Hi,
>
> See the code below.
> My question is: Why the cast worked, considering the c variable is a
> TAClass instance, not a TBClass.

As long as you don't access fields of TBClass inside of GetInfoA you 
won't run into problems, because FInfoA is accessed using an offset. If 
you want to catch such problems you should compile with "-CR". The 
output will then look like the following:

=== output begin ===

An unhandled exception occurred at $080482E8 :
EInvalidCast : Invalid type cast
   $080482E8

=== output end ===

As a sidenote: as long as you don't access an object member at all you'd 
even be able to cast a primitive type like Integer to an object and call 
methods on it... (not that I'd suggest to do this... ;) )

Regards,
Sven



More information about the fpc-pascal mailing list