【发布时间】:2019-02-23 22:34:03
【问题描述】:
是否可以从基类中释放超类中的对象?
interface
type
Tbase = class(TObject)
public
o: TObject;
constructor Create;
destructor Destroy; override;
end;
Tsup = class(Tbase)//there will be lot more classes based on Tbase
public
o: TStrings;
constructor Create;
end;
implementation
procedure main();
var
a: Tsup;
begin
a := Tsup.Create;
//
a.Free;
end;
constructor Tbase.Create;
begin
inherited;
end;
destructor Tbase.Destroy;
begin
if o <> nil then
o.Free;
inherited;
end;
constructor Tsup.Create;
begin
inherited;
o := TStringList.Create;
end;
destructor Tsup.Destroy;
begin
// let tbase to free o ?
inherited;
end;
object o 不会在 tbase 类中使用(除了释放)
tsup class var o type 不同于 tbase cass var o type
是否可以在 tbase.Destroy() 中释放 tsup.o ? (所以我可以避免实现 析构函数 Tsup.Destroy;)
谢谢。
【问题讨论】:
标签: delphi