2012-06-09 71 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

不错 - 我从来不知道它可以用来解决这些问题! –

相关问题