开发者

How to create an instance that can not be destroyed

Imagine I have a class: TCantBeDestroyed

Some one knows how to create a kind of instance that can't be destroyed in both cases, directly: CantBeDestroyed.Free; CantBeDestroyed.Destroy;

And can't be destroyed by ca开发者_如何学Pythonst: TObject(CantBeDestroyed).Free; TObject(CantBeDestroyed).Destroy;

Tks.


You can't stop anyone calling a destructor or Free, but you can make sure that doing so has no effect:

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;

I can't imagine why you would want to do this though!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜