Do the two entries have the same meanings?

I asked another question about using records with statements. During testing, I discovered an anomaly when two instances of this type seem to use the same memory.

There is an Integer array in the record ...

type
  TVersion = record
    Values: array of Integer;
    function Count: Integer;
    class operator implicit(aVersion: TVersion): String;
    class operator implicit(aVersion: String): TVersion;
  end;

class operator TVersion.implicit(aVersion: TVersion): String;
var
  X: Integer;
begin
  Result:= '';
  for X := 0 to Length(aVersion.Values)-1 do begin
    if X > 0 then Result:= Result + '.';
    Result:= Result + IntToStr(aVersion.Values[X]);
  end;
end;

class operator TVersion.implicit(aVersion: String): TVersion;
var
  S, T: String;
  I: Integer;
begin
  S:= aVersion + '.';
  SetLength(Result.Values, 0);
  while Length(S) > 0 do begin
    I:= Pos('.', S);
    T:= Copy(S, 1, I-1);
    Delete(S, 1, I);
    SetLength(Result.Values, Length(Result.Values)+1);
    Result.Values[Length(Result.Values)-1]:= StrToIntDef(T, 0);
  end;
end;

function TVersion.Count: Integer;
begin
  Result:= Length(Values);
end;

Now I'm trying to implement this ...

var
  V1, V2: TVersion;
begin
  V1:= '1.2.3.4';
  V2:= V1;
  ShowMessage(V1);
  ShowMessage(V2);
  V2.Values[2]:= 8;
  ShowMessage(V1);
  ShowMessage(V2);
end;

I expect V2to be 1.2.8.4and V1stay 1.2.3.4. However, it V1also changes to 1.2.8.4.

How to save these records independently when I assign them?

+3
source share
1 answer

. - . , . , , , .

:

X Y , X: = Y X , Y. ( X .) , copy-on-write , .

, , . (, ..), , ..

:

  • .
  • .
  • setter , .

- . , SetLength:

SetLength(arr, Length(arr));

promises, SetLength , , .

copy-on-write. , copy-on-assign, .

, :

, ?

- , . copy-on-write.

+4

All Articles