[fpc-devel] Forward Record
    Sven Barth 
    pascaldragon at googlemail.com
       
    Thu Dec  5 11:25:09 CET 2024
    
    
  
Amir via fpc-devel <fpc-devel at 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20241205/92204325/attachment.htm>
    
    
More information about the fpc-devel
mailing list