2013-04-21 42 views
1

返回它这个函数创建TEDIT类型的控制对象,并将其返回到它发生的代码的任何行从调用:创建一个控制对象和函数在Delphi

function TBaseForm.CreateEdit(AOwner: TWinControl; 
           inTop, inLeft, inWidth: integer) : TEdit; 
var 
    edt: TEdit; 
begin 
    edt := TEdit.Create(AOwner); 
    with edt do begin 
     Parent := AOwner; 
     Width := inWidth; 
     Top := inTop; 
     Left := inLeft; 
    end; 
    result := edt; 
end; 

在后来代码:

edtTitle := CreateEdit(Self, 20, 90, 300); 

现在。函数中的代码没有任何问题吗?例如,有什么东西没有挂在内存的某个地方?

回答

1

没有内存泄漏。

你可以做到这一点更干净(尽管我仍然不知道为什么你这样做),并避免额外的变量edt

function TBaseForm.CreateEdit(AOwner: TWinControl; 
           inTop, inLeft, inWidth: integer) : TEdit; 
begin 
    Result := TEdit.Create(AOwner); 
    Result.Parent := AOwner; 
    Result.Width := inWidth; 
    Result.Top := inTop; 
    Result.Left := inLeft; 
end; 
+2

你应该解释为什么* *是没有泄漏。 – 2013-04-21 07:41:34