人肉保证,Delphi中的out参数

王朝delphi·作者佚名  2006-11-04
窄屏简体版  字體: |||超大  

Delphi里面,用关键字var或out,可以让函数参数按传递引用,奇怪的是,var和out根本没有本质上的区别,按官方的说法:

An out parameter, like a variable parameter, is passed by reference. With an out parameter, however, the initial value of the referenced variable is discarded by the routine it is passed to. The out parameter is for output only; that is, it tells the function or procedure where to store output, but doesn't provide any input.

看上去,好象是说过程里不能去读out参数的值,因为它的初始值是不可知的,但实际上如果你读了,编译器连个警告都没有,而且out参数也并不是被调用者事先清零(象C#那样),它唯一的用处就是让写过程的程序员警惕一下,这个参数没有什么初始值的,只能给它赋值,而永远不要去用它的值……这一切只能靠程序员人肉保证,编译器一点忙都帮不上。

{ 没有一点警告 }

var

tmp: Integer;

procedure Foo(out v: Integer);

begin

tmp := v;

v := 1;

end;

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有  導航