Typecast class-of to class

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Typecast class-of to class

Mattias Gaertner
Hi,

FPC allows to type cast a class-of variable to a class instance. Is
this a bug or a feature?

var
  c: TClass;
begin
  TObject(c).ClassName; // crash
end.
 

Mattias
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Typecast class-of to class

Marco van de Voort
In our previous episode, Mattias Gaertner said:
>
> FPC allows to type cast a class-of variable to a class instance. Is
> this a bug or a feature?
>
> var
>   c: TClass;
> begin
>   TObject(c).ClassName; // crash
> end.

Feature (allowed because equal in size), used in e.g. Registerxxx functions to add class references to the
.objects part of tstringlist. Delphi has no container type for references
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Typecast class-of to class

Jonas Maebe-3
In reply to this post by Mattias Gaertner


Mattias Gaertner wrote:
> FPC allows to type cast a class-of variable to a class instance. Is
> this a bug or a feature?
>
> var
>   c: TClass;
> begin
>   TObject(c).ClassName; // crash
> end.

With -CR you should get an object check error instead.


Jonas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal