[問題] set function傳值

看板C_and_CPP (C/C++)作者 (★)時間14年前 (2012/05/13 22:17), 編輯推噓0(0012)
留言12則, 3人參與, 最新討論串1/1
Account.h http://pastie.org/3905112 Account.cpp http://pastie.org/3905116 Withdraw.h http://pastie.org/3905117 Withdraw.cpp http://pastie.org/3905123 請問為什麼我Withdraw.cpp中第30和31行的set function呼叫了之後 Account.cpp中available和total的值還是沒有改變? 如果在set function裡面輸出的話是修改過的值 可是在Account.cpp的mainMenu輸出的話就是原本的值 請問是什麼原因? 謝謝。 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 218.167.191.193

05/14 00:36, , 1F
void Account::setAvailableBalance(const double A)
05/14 00:36, 1F

05/14 00:37, , 2F
or available=*A; 猜的
05/14 00:37, 2F

05/14 03:54, , 3F
Withdraw::Withdraw() 裡面呼叫 Withdraw::menu()
05/14 03:54, 3F

05/14 03:54, , 4F
而裡面你作修改的對象是 returnValue : Account, 一個
05/14 03:54, 4F

05/14 03:55, , 5F
物件, 當然可以改到區域物件, 對於一個在
05/14 03:55, 5F

05/14 03:56, , 6F
Account::mainMenu() line 81 建構的暫時 Withdraw 物
05/14 03:56, 6F

05/14 03:57, , 7F
件而言, 他並不擁有任何需要 set 的 Account 物件參考
05/14 03:57, 7F

05/14 03:57, , 8F
所以改不到你當前的物件
05/14 03:57, 8F

05/14 09:29, , 9F
謝謝解答
05/14 09:29, 9F

05/14 09:50, , 10F
那請問要用什麼方法才能把值傳過去?
05/14 09:50, 10F

05/14 10:08, , 11F
keyword: pass by reference
05/14 10:08, 11F

05/14 12:12, , 12F
可是我用的不就是pass by reference了?
05/14 12:12, 12F
文章代碼(AID): #1Fhy9VUd (C_and_CPP)
文章代碼(AID): #1Fhy9VUd (C_and_CPP)