2017-04-21 49 views
-1

我想实现一个类,其实例名为toto的属性是一个动态数组我可以追加,读取,写入像一个tlist。 我写这个:在D6什么是一个类的动态数组作为属性的最短的实现是什么

Tmodel = Class(TObject) 
     ftoto:array of single; 
     function gettoto(ind : integer):single 
     function gettotosize:integer; 
     procedure settoto(ind : integer;valeur:single); 
     property toto[ind:integer]:single read gettoto write settoto; 
     property totosize:integer read gettotosize; 
    end; 


    function Tmodel.gettoto(ind : integer):single; 
    begin 
    result:=ftoto[ind]; 
    end; 
    procedure Tmodel.settoto(ind : integer;valeur:single); 
    begin 
    setlength(ftoto,ind+1); 
    ftoto[ind]:=valeur; 
    end; 
    function Tmodel.gettotosize:integer; 
    begin 
    result:=length(ftoto); 
    end; 

它是正确的吗?

回答

0

在您的toto属性设置器中调用SetLength()是错误的。改变你的totosize属性,而是给它一个setter来调整数组的大小。

type 
    TModel = class(TObject) 
    private 
    fToto: array of Single; 
    function GetToto(index: Integer): Single; 
    function GetTotoSize: Integer; 
    procedure SetToto(index: integer; value: Single); 
    procedure SetTotoSize(value: Integer); 
    public 
    property Toto[index: Integer]: Single read GetToto write SetToto; 
    property TotoSize: Integer read GetTotoSize write SetTotoSize; 
    end; 

function TModel.GetToto(index: Integer): Single; 
begin 
    Result := fToto[index]; 
end; 

procedure TModel.SetToto(index: Integer; value: Single); 
begin 
    fToto[index] := value; 
end; 

function TModel.GetTotoSize: Integer; 
begin 
    Result := Length(fToto); 
end; 

procedure TModel.SetTotoSize(value: Integer); 
begin 
    SetLength(fToto, value); 
end; 
+0

那么析构函数呢?我应该添加一个freeandnil来避免泄漏? – bbd

+0

而不是猜测,阅读文档,它会告诉你,动态数组管理 –

+0

是否有可能DH不回答我的帖子,因为它总是负面和无用的评论。预先提示: – bbd

相关问题