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.
In this case, however, my array is a field of a record:
TMyRec = record
Value: integer;
&Array: array of integer;
end;
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.
To solve that sort of problem I decided to overload the assign operator as follows:
TMyRec = record
Value: integer;
&Array: array of integer;
public
class operator Implicit(Value: TMyRec): TMyRec;
end;
class operator TMyRec.Implicit(Value: TMyRec): TMyRec;
begin
Result := Value;
Result.&Array := Copy(Value.&Array);
end;
If this worked, I wouldn't have to copy all array fields in my records separately after assigning TMyRecord variables one to another.
Here is what I do:
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;
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?