type Tmyclass = class(TObject)
somearray: array of TSometype
FBool: Boolean;
Fint: Integer;
Fstr: string;
constructor Create;
destructor Destroy; override;
end;
implementation
constructor Tmyclass.Create;
begin
inherited;
SetLength(somearray,0); //is this needed?
end;
destructor TmyClass.Destroy;
begin
SetLength(somearray,0); //this IS needed!
inherited;
end;
还有哪些类型在创建时被初始化?例如我在类里面声明的内容。
FBool 一定是假的吗?
FInt 是否保证为 0?
Fstr 保证是''吗?
本地呢?只有字符串?
我使用德尔福 XE。
最佳答案
所有属于对象的内存在对象构造(堆分配)时都被初始化为 0,所以通常你不需要初始化属于一个类的任何东西,默认值(零内存)是:
等等。
对于局部变量和与堆栈相关的任何内容,它将包含垃圾,因此您有责任在使用变量之前提供有意义的值。
就像是:
procedure Something;
var
x: Integer;
begin
ShowMessage(IntToStr(X));
end;
将显示一个随机值。
关于delphi - 我需要在初始化时设置动态数组吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5314918/