delphi customclass通过引用和解引用指针深度克隆

时间:2017-10-03 14:18:14

标签: delphi pointers cloning

您好我想要深度克隆包含其他子类的自定义类,这些子类太多而无法通过assign()方式支持。

为简化起见,我使用RTTI创建实例并使用我的切换程序

克隆OriginalInlineContent:TInlineContent;

NewInstance := TInlineContent.CreateInstance(OriginalInlineContent, ...)//with RTTI and instance is good to go this returns TInlineContent

现在我想切换OriginalInlineContent和NewInstance的引用

class procedure TInlineContent.SwitchMemory(Source, Destination:
    TInlineContent);
var P, S : ^TInlineContent;
    SourceSize, DestinationSize : integer;
begin
  Assert(Source <> nil);
  Assert(Destination <> nil);

  SourceSize := SizeOf(Source);
  DestinationSize := SizeOf(Destination);

  P := @Source;
  S := @Destination;
  Destination := @P;   **//possibly replace destination with P means @S is scrambled?**
  Source := @S;

  Assert(SizeOf(Source) = DestinationSize);
  Assert(SizeOf(Destination) = SourceSize);
end;

它似乎适用于源而非目标。我该如何实现这一目标?

0 个答案:

没有答案