來源:互聯網網民 2006-11-04 12:44:05
評論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;
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;