全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1154
推到 Plurk!
推到 Facebook!

out保留字的作用?

尚未結案
tewcc
一般會員


發表:3
回覆:4
積分:1
註冊:2003-07-12

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-12-24 08:54:20 IP:211.23.xxx.xxx 未訂閱
對不起,我是個新手,請各位高手指教: 按照delphi的線上說明: 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. 如果我如下的寫法: procedure test(out s : Integer); begin s:=s 1; end; procedure TForm1.Button1Click(Sender: TObject); var t : Integer; begin t:=7; test(t); ShowMessage(IntToStr(t)); end; 按button1後,應不會彈出數字8才對,但實際上反而彈出數字8,這結果好像和線上說明講的不符; 另外如果我用文字串來做類似測試的話: procedure test(out s : String); begin s:=s '1'; end; procedure TForm1.Button1Click(Sender: TObject); var t : String; begin t:='7'; test(t); ShowMessage(t); end; 郤會符合線上說明所說的,彈出字串"1",而不是字串"71"; 這兩種不同的結果,在我看來是互相矛盾的,也把我給搞混,有人能說明一下嗎?
00156
高階會員


發表:45
回覆:195
積分:112
註冊:2002-06-01

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-12-24 10:00:57 IP:61.56.xxx.xxx 未訂閱
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. But you're not using MyRecord to pass any data to the GetInfo procedure; MyRecord is just a container where you want GetInfo to store the information it generates. The call to GetInfo immediately frees the memory used by MyRecord, before program control passes to the procedure. Out parameters are frequently used with distributed-object models like COM and CORBA. In addition, you should use out parameters when you pass an uninitialized variable to a function or procedure. 我想out只是簡單提供輸出的container,既然不提供輸入功用,就不需要確保起始值被清空。如果要初始化參數,還是用參數傳遞吧!
tewcc
一般會員


發表:3
回覆:4
積分:1
註冊:2003-07-12

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-12-24 13:16:22 IP:218.165.xxx.xxx 未訂閱
多謝大大的回答,但我不是在測試"如果要初始化參數,還是用參數傳遞吧",我是因為這句話: the initial value of the referenced variable is discarded by the routine it is passed to 才對我之前那兩個例子的不同結果產生疑惑; 我之前因為實在看不出var和out的差別,讀了線上說明,才做這兩個例子來測試的;只是沒想到測試的結果會互相矛盾,所以才在這裡發問的. 因為如果"只是簡單提供輸出的container",將這兩個例子裡的out都換成var的話,除了第二個例子的結果不同外,我還是搞不清使用out和var會有何差別?
00156
高階會員


發表:45
回覆:195
積分:112
註冊:2002-06-01

發送簡訊給我
#4 引用回覆 回覆 發表時間:2003-12-24 17:44:17 IP:61.56.xxx.xxx 未訂閱
我找到了下面一段說明:    from the D3 help ..    An out parameter is used when the called procedure or function parameter  will exclusively return output data. Out parameters are resource managed; an out parameter reference is freed automatically by the compiler when the procedure or function is called. When a method with an out parameter is called, it simply fills the parameter with new information and on failure will set the parameter to nil. Note: It is recommended that you only use out parameters with COM methods since variable parameters are more efficient than out parameters. For information on COM, see "Interfaces." In all other respects an out parameter behaves like a variable parameter. out參數只是被設計用來存放輸出值(不含輸入哦,所以初始值並沒有被保證是可靠的,因為已經假定你使用out變數只會"寫入"值,不會"取出"...)。因此,你測試中的這行敘述已經超越out的規範:
s:=s+1; //紅字部份已經"取值"了
另外,上面的說明只略微提到compiler如何清除out參數的原則。我也做了你的測試,並且加入了其他型態的變數測試(double,byte...),結果只有string的實際內容會被清除,不知道是不是為了效率考量?有趣的是,如果自訂型態裡包含string與integer,結果string會被清除、integer還是會保留原有的值。 至於和var變數的差異,上面的Note部份有提及。希望個人淺見能提供您參考。 發表人 - 00156 於 2003/12/24 17:47:56
tewcc
一般會員


發表:3
回覆:4
積分:1
註冊:2003-07-12

發送簡訊給我
#5 引用回覆 回覆 發表時間:2003-12-24 19:22:39 IP:218.165.xxx.xxx 未訂閱
先感謝00156大大的熱情回答,但總覺得out保留字的這種行為怪怪的; "the initial value of the referenced variable is discarded by the routine it is passed to"; "an out parameter reference is freed automatically by the compiler when the procedure or function is called" 從這兩段文字看來,只有第二個例子才符合這個特性(或者說只有string類型的參數,按照00156大大的測試結果),因為按照前面兩段英文來看,參數的起始值在函數被調用時已被discarded或freed了,所以看來該兩段文字只適用於string類型的參數?
change.jian
版主


發表:29
回覆:620
積分:439
註冊:2003-06-02

發送簡訊給我
#6 引用回覆 回覆 發表時間:2003-12-25 10:57:49 IP:61.222.xxx.xxx 未訂閱
引言: 先感謝00156大大的熱情回答,但總覺得out保留字的這種行為怪怪的; "the initial value of the referenced variable is discarded by the routine it is passed to"; "an out parameter reference is freed automatically by the compiler when the procedure or function is called" 從這兩段文字看來,只有第二個例子才符合這個特性(或者說只有string類型的參數,按照00156大大的測試結果),因為按照前面兩段英文來看,參數的起始值在函數被調用時已被discarded或freed了,所以看來該兩段文字只適用於string類型的參數?
我倒是認為會規範圍一個out的保留字,其實只是為了讓程式碼更清楚.如果我把參數宣告為var,那麼維護這個procedure的人(假設維護程式的人並不是當初寫這隻程式的人)就會考量到也許傳入的參數也有作用.但若宣告為out時,那麼這個參數很明顯的,就是為了輸出結果.當然,如果把out宣告為var也是可以用,但維護程式時,用var就不如out來的更明顯,更能清楚表達這procedure的功能吧. 小弟淺見
系統時間:2024-04-26 15:52:39
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!