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:
- 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.
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
TMyRec.Clone
function instead. – StigInner_Array
immutable; code likeA.Inner_Array[1] := 1;
will be forbidden - any write toInner_Array
should create a new array instance. Read also sergworks.wordpress.com/2013/04/10/… for some more hints. – Guimond