2012-06-09 64 views

回答

18

最大的區別是,當方法的參數不正確時,對於非重載方法,錯誤消息明顯更好。

program Test; 

procedure F(X: Integer); 
begin 
end; 

procedure G(X: Integer); overload; 
begin 
end; 

var 
    P: Pointer = nil; 

begin 
    F(P); // E2010 Incompatible types: 'Integer' and 'Pointer' 
    G(P); // E2250 There is no overloaded version of 'G' that can be called with these arguments 
end. 

更微妙的是,一個重載的方法可能會重載您不知道的函數。考慮標準IfThen函數。 StrUtils.IfThen存在恰好一次:

function IfThen(AValue: Boolean; const ATrue: string; 
    AFalse: string = ''): string; overload; inline; 

但它被標記爲overload。這是因爲它過載了Math.IfThen,並且如果單個設備同時使用MathStrUtils,則不合格的IfThen將根據參數解析爲正確的功能,並且不管uses列表中設備的順序如何。

+3

不錯 - 我從來不知道它可以用來解決這些問題! –

相關問題