带有自定义初始化和最终化代码的记录

带有自定义初始化和最终化代码的记录

type
  TMyRecord = record
    Value: Integer;
    class operator Initialize(out Dest: TMyRecord);
    class operator Finalize(var Dest:TMyRecord);
  end;

class operator TMyRecord.Finalize(var Dest: TMyRecord);
begin
  Form1.Memo1.Lines.Add('destroyed ' + IntToHex(IntPtr(@Dest)));
end;

class operator TMyRecord.Initialize(out Dest: TMyRecord);
begin
  Dest.Value := 10;
  Form1.Memo1.Lines.Add('created ' + IntToHex(IntPtr(@Dest)));
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  var t: TMyRecord;
  Caption := t.Value.ToString;    //10
end;

 

posted @ 2025-08-31 13:50  delphi中间件  阅读(4)  评论(0)    收藏  举报