title: Can you overload the assignment operator for Delphi records?
body: <p>I have a record that contains a dynamic array. It is normal that when you assign one array variable to another, in fact only the pointer to that array is assigned. This means that when you do that, both the variables point to the same array until you change the size of one of them. Therefore, when I want to assign a separate copy of an array to a variable, I use the Copy() function.</p>
  
  <p>In this case, however, my array is a field of a record:</p>
  
  <pre><code>  TMyRec = record
      Value: integer;
      &amp;Array: array of integer;
    end;
  </code></pre>
  
  <p>When I declare two variables of type TMyRec and then assign one to another, the "Array" fields in both of the records will be pointing to the same address in memory.</p>
  
  <p>To solve that sort of problem I decided to overload the assign operator as follows:</p>
  
  <pre><code>TMyRec = record
    Value: integer;
    &amp;Array: array of integer;
  public
    class operator Implicit(Value: TMyRec): TMyRec;
  end;
  
  class operator TMyRec.Implicit(Value: TMyRec): TMyRec;
  begin
    Result := Value;
    Result.&amp;Array := Copy(Value.&amp;Array);
  end;
  </code></pre>
  
  <p>If this worked, I wouldn't have to copy all array fields in my records separately after assigning TMyRecord variables one to another.</p>
  
  <p>Here is what I do:</p>
  
  <pre><code>var
    Rec1, Rec2: TMyRec;
  begin
    Rec1.Value := 10;
    SetLength(Rec1.Array, 1);
  
    //I expected the "Implicit" method to be invoked here (but it is not...)
    Rec2 := Rec1;
  
    //if I do that, the Rec1.Array[0] will also be changed to 1 - I don't want that to happen
    Rec2.Array[0] := 1;
  end;
  </code></pre>
  
  <p>Is there a way to make my operator overload work as I want it to? The thing is that I'm trying to overload the default assignment operator. Isn't that possible?</p>