Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to overload assign operator for record in Delphi

I want to make the type of record that uses dynamic arrays.

Using the variables A and B of this type I want to be able to perform operations A: = B (and other) and be able to modify the content of A without modification B like in snipped 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:

  1. when I don't use overriding assigning operator in my TMyRec, A:=B means A and B (their Inner_Array) are pointing the same place in memory.
  2. to avoid problem 1) I want to overload assign operator using:

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

but compilator (Delphi XE) says:

[DCC Error] : E2521 Operator 'Implicit' must take one 'TMyRec' type in parameter or result type

How to resolve this problems. I read several similar posts on stackoverflow but they don't work (if I understood they well) on my situation.

Artik

like image 689
Artik Avatar asked Jul 31 '13 20:07

Artik


People also ask

How do you overload an assignment operator?

Overloading the assignment operator (operator=) is fairly straightforward, with one specific caveat that we'll get to. The assignment operator must be overloaded as a member function. This will call f1. operator=(f1), and under the simplistic implementation above, all of the members will be assigned to themselves.

What is the use of overloading an assignment (=) operator?

Assignment Operators Overloading in C++ You can overload the assignment operator (=) just as you can other operators and it can be used to create an object just like the copy constructor. Following example explains how an assignment operator can be overloaded.

What is assignment overload?

Overload assignment is an assignment for extra pay in excess of the normal assignment of a full- time unit member.

What is operator in Delphi?

Operators. Operators behave like predefined functions that are part of the Delphi language. For example, the expression (X + Y) is built from the variables X and Y , called operands, with the + operator; when X and Y represent integers or reals, (X + Y) returns their sum.


1 Answers

It is not possible to overload the assignment operator. This means that what you are attempting to do is not possible.


Edit: It is possible now - http://docwiki.embarcadero.com/RADStudio/Sydney/en/Custom_Managed_Records#The_Assign_Operator

like image 125
David Heffernan Avatar answered Sep 28 '22 09:09

David Heffernan