[問題] 回傳refernce return reference type

看板C_and_CPP作者 (你今天浪費時間了嗎?)時間14年前 (2012/02/13 10:25), 編輯推噓0(005)
留言5則, 2人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) NetBeans 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) No *[36m程式碼(Code):(請善用置底文網頁, 記得排版) *[m http://codepad.org/mqcg09rM 問題(Question): 這是我執行結果的後半部 : I've got another cool idea.... b's addr : 0x28cc90 b = 0 After calling retRef... temp1's addr : 0x28cc64 b's addr : 0x28cc90 b = 10 After calling retVal... temp2's addr : 0x28cc64 b's addr : 0x28cc90 b = 20 How about use a reference variable ? temp1's addr : 0x28cc64 c's addr : 0x28cc64 c = 1629085019 1.請問如何解釋三個function呼叫時,建立的暫存變數都是用同一個addr. 是因為Stack 的 activation record 都是同一個位置起始? 2.line 68 : b = retRef( ); 請問為什麼int 可以收 int& 而且值還不會像 line 79 : int &c = retRef( ); c值會不正確 copy ctor? 感謝版上的大大們 解惑! -- 什麼事都等到明天才做 明天你就葛屁了 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 221.120.66.10 ※ 編輯: rnbjacky 來自: 221.120.66.10 (02/13 10:26)

02/13 10:49, , 1F
第一個問題就像你講的
02/13 10:49, 1F

02/13 10:52, , 2F
第二個問題是因為b是int,會做一次複製動作
02/13 10:52, 2F

02/13 10:53, , 3F
C是int &,它會參考到函式的區域變數,但離開函式就無效
02/13 10:53, 3F

02/13 19:48, , 4F
我想問的是int 為什麼可以收int& compiler做了什麼事
02/13 19:48, 4F

02/13 20:21, , 5F
int x=10; int *p=&x; int y=*p; 類似這樣的動作
02/13 20:21, 5F
文章代碼(AID): #1FE7I6ud (C_and_CPP)