[fpc-devel] Operator overloading question: Impossible to overload assignment for equal types. Why?
Bart
bartjunk64 at gmail.com
Thu Apr 25 11:47:42 CEST 2024
On Thu, Apr 25, 2024 at 11:18 AM Sven Barth via fpc-devel
<fpc-devel at lists.freepascal.org> wrote:
> 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).
OK, that makes sense.
> 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
So, I would have to declare something like this?
class operator TArr.Copy(constref aSrc: TArr; var aDst: TArr);
begin
aDst.A := Copy(aSrc.A);
end;
And then in my example:
R1.A := [1,2,3];
R2 := R1;
--> R2.A will be (1,2,3), and changing that will not affect R1.A anymore
Sounds like a nice feature to have (in 3.4).
I'll write some tests to get familiar with this and if I don't forget
I'll write a simple example in the wiki (there's a ToDo for that
currently).
--
Bart
More information about the fpc-devel
mailing list