Re: [問題] 請問call by address

看板java作者 (yago)時間19年前 (2007/03/31 19:48), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串11/11 (看更多)
※ 引述《tkcn (小安)》之銘言: : 下面是 C# 的程式碼, : --- : static void callByAddr(ref string param) { : Console.WriteLine("before change: " + param); : param = "abc"; : Console.WriteLine("after change: " + param); : } : static void Main(string[] args) { : string str = "123"; : Console.WriteLine("before call: " + str); : callByAddr(ref str); : Console.WriteLine("after call: " + str); : } : --- : output: : --- : before call: 123 : before change: 123 : after change: abc : after call: abc : --- : str 是我要傳遞的參數, : 因為使用了 ref 修飾,因此傳遞的內容是 str 的 address。 : 所以當我 assign 其他的值給 param,同時也會影響 str, : 因為 str 與 param 其所 bind 的記憶體位址相同。 : 補充一點個人淺見, : 坊間一些 C/C++ 書籍會將 foo( &obj ) 認定為 call by address, : 我想這是因為真正要傳遞的參數是 obj (我並非指實作面) : 而傳遞 address 只是一種為了達到 side-effect 手段, : 因此其實我認為,稱呼這種情況為 call by address 其實也未嘗不可。 基本上 這個問題之前在language版就有人討論過了 只是不知道ptt的板被怎樣洗掉了 基本上如果記得沒錯的話 之前是說 call by address 是坊間一些 C/C++書籍觀念錯誤亂寫的 所以害得學校老師也認為這樣 所以慢教 搞的一堆人認為有call by address -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.125.32.163

03/31 20:06, , 1F
相關討論已經轉去 PLT 版囉~
03/31 20:06, 1F
文章代碼(AID): #163afmc9 (java)
討論串 (同標題文章)
文章代碼(AID): #163afmc9 (java)