Amir via fpc-devel <fpc-devel@lists.freepascal.org> schrieb am Do., 5. Dez.
2024, 00:57:

>   I wonder why? Is there any technical difference between classes and
> records for the purpose of forward declaration?
>

Yes, there is. Classes are implicit pointer types, thus when you have a
field that is of type class then it's size will always be the size of a
Pointer. Records (and TP-style objects) have a different size depending in
their fields and this size needs to be known when you use a record (or
TP-style object) directly as a field.


> My use-case was to define a few records like the following:
>
> type
>   TData = record
>     ....
>     function ToA: TA;
>    end;
>
>   TA = record
>      Data: TData;
>      ....
>   end;
>
> I know I can define classes instead of record, but I do not want to do so,
> since I need to "Free" the objects.
>

You could add a record helper below the declaration of TA for TData instead
of declaring ToA inside of TData:

=== code begin ===

  TDataHelper = record helper for TData
    function ToA: TA;
   end;

=== code end ===

The usage is the same as if you had declared the method inside of TData.

Regards,
Sven

>
_______________________________________________
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

Reply via email to