[fpc-devel] Operator overloading question: Impossible to overload assignment for equal types. Why?

Sven Barth pascaldragon at googlemail.com
Thu Apr 25 11:18:27 CEST 2024


Bart via fpc-devel <fpc-devel at lists.freepascal.org> schrieb am Mi., 24.
Apr. 2024, 11:59:

> Overloading the := (assignment) operator for equal types is forbidden.
> Out of curiosity I would like to know why that is?
>

If there exists a built-in operator then overloading that operator is
forbidden to avoid confusion when what operator might be used (operators
can after all be declared *anywhere* and thus they might sometimes be used
and sometimes not depending on which unit is used).


> When you have e.g. a record definition containing a dynamic array, not
> being able to overload the assignment operator is just a PITA.
>

Then declare a Copy management operator in the record. Unlike an assignment
operator these *must* be part of the record, thus the same rules are
applied for the same type everywhere unlike for operator overloads. See
also https://wiki.freepascal.org/management_operators

Regards,
Sven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20240425/ff002f6c/attachment.htm>


More information about the fpc-devel mailing list