看板 C_and_CPP 關於我們 聯絡資訊
自己摸索了C++一陣子,還不太會用class 找了很久但是真的找不到問題,cout<<a.y輸出的永遠不是3 請問問題到底出在哪裡? #include <iostream> using namespace std; class A { public: A(); int y; }; int main() { A a; cout <<a.y; return 0; } A::A(){ int y=3; cout <<y<<endl; } 結果是 3 24 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.112.25.112 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1506570855.A.F60.html
bluesoul: int y = 3; -> y = 3; 09/28 12:02
TianBonBon: variable scope 09/28 12:16
birka1222: 謝謝你!!!! 09/28 12:19
peterwu4: int y=3; 下面加一行 this->y=y; 此y非彼y 09/28 12:38
ilikekotomi: 我們team有規定成員變數後面要加個底線 可以參考看看 09/28 19:53
TitanEric: 樓上的意思是member variable與local variable不一樣 09/30 00:21
TitanEric: 做個區別 09/30 00:21