如何创建无法销毁的实例

时间:2011-04-12 18:58:12

标签: delphi

想象一下,我有一个班级:TCantBeDestroyed

有人知道如何在两种情况下直接创建一种无法销毁的实例:  CantBeDestroyed.Free;  CantBeDestroyed.Destroy;

并且不能被施法者摧毁:  TObject的(CantBeDestroyed)。自由;  TObject的(CantBeDestroyed).Destroy;

韩国社交协会

1 个答案:

答案 0 :(得分:5)

你无法阻止任何人调用析构函数或Free,但你可以确保这样做没有效果:

type
  TCannotBeDestroyed = class
  public 
    destructor Destroy; override;
    procedure BeforeDestruction; override;
    procedure FreeInstance; override;
  end;

destructor TCannotBeDestroyed.Destroy;
begin
  //don't call inherited
end;

procedure TCannotBeDestroyed.BeforeDestruction;
begin
  //don't call inherited
end;

procedure TCannotBeDestroyed.FreeInstance;
begin
  //don't call inherited
end;

我无法想象你为什么要这么做!