How to overload the destination statement for writing in Delphi

I want to create a record type that uses dynamic arrays.

Using variables A and B of this type, I want to be able to perform operations A: = B (and others) and be able to change the contents of A without modifying B, as in the abbreviated code below:

type TMyRec = record Inner_Array: array of double; public procedure SetSize(n: integer); class operator Implicit(source: TMyRec): TMyRec; end; implementation procedure TMyRec.SetSize(n: integer); begin SetLength(Inner_Array, n); end; class operator TMyRec.Implicit(source: TMyRec): TMyRec; begin //here I want to copy data from source to destination (A to B in my simple example below) //but here is the compilator error //[DCC Error] : E2521 Operator 'Implicit' must take one 'TMyRec' type in parameter or result type end; var A, B: TMyRec; begin A.SetSize(2); A.Inner_Array[1] := 1; B := A; A.Inner_Array[1] := 0; //here are the same values inside A and B (they pointed the same inner memory) 

There are two problems:

  • when I do not use the override assignment operator in my TMyRec, A: = B means that A and B (their Inner_Array) point to the same place in Memory.
  • to avoid the problem 1) I want to overload the destination statement with:

    class operator TMyRec.Implicit (source: TMyRec): TMyRec;

but the compiler (Delphi XE) says:

[DCC Error]: E2521 The Implicit operator must accept one type of TMyRec in the parameter or result type

How to solve these problems. I read several similar posts about stackoverflow, but they do not work (if I understand them well) in my situation.

Artik

+4
source share
1 answer

Cannot overload assignment operator. This means that what you are trying to do is impossible.

+4
source

Source: https://habr.com/ru/post/1494571/


All Articles