[fpc-pascal] Type-casting a class variable

Olivier Sannier olivier at obones.com
Wed May 1 17:31:30 CEST 2024


Hello,

You should cast to TWindowClass as TWindow is used to cast an instance, 
not a class reference.

Note that you may not have TWindowClass, in which case you need to 
declare it like this:

type
     TWindowClass = class of TWindow;

Note that CreateNewWindow would have to be virtual for derived clases 
methods to be called.


Le 01/05/2024 à 16꞉28, Adriaan van Os via fpc-pascal a écrit :
> Suppose I have a
>
>     var myClass: TClass
>
> and (for example) a
>
>     class function TWindow.CreateNewWindow( ....)
>
> Now I want to call CreateNewWindow for var myClass. Of course, 
> depending on the class, CreateNewWindow will behave different. 
> Type-casting myClass to TWindow crashes (in my setup). And even if it 
> worked, wouldn't it loose the class information ?
>
> Of course, by adding an extra parameter of type TClass to 
> CreateNewWindow, the issue can be solved. But that's far from elegant.
>
> How to solve this ? Anything I missed ?
>
> Regards,
>
> Adriaan van Os
>
> _______________________________________________
> fpc-pascal maillist  - fpc-pascal at lists.freepascal.org
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20240501/439157e9/attachment.htm>


More information about the fpc-pascal mailing list