[問題] constructor的問題

看板C_and_CPP作者時間9年前 (2014/12/16 10:13), 9年前編輯推噓0(002)
留言2則, 1人參與, 最新討論串2/2 (看更多)
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) g++ 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 小弟是初學者,嘗試寫nested class,class B為class A 的class member。其中A的const ructor如果放在initilization list裡會有預期的100 100;但放在function boy裡會 100 10的錯誤,想了解問題在哪? 餵入的資料(Input): 預期的正確結果(Expected Output): 100 100 錯誤結果(Wrong Output): 100 10 程式碼(Code):(請善用置底文網頁, 記得排版) 1 #include<iostream> 2 using namespace std; 3 class A 4 { 5 public: 6 A(int ia) 7 { 8 B Ted(ia); 9 i=ia; 10 解問疆11 private: 13 int i; 14 class B 15 { 16 public: 17 B() 18 { 19 j=10; 20 } 21 B(int jb) 22 { 23 j=jb; 24 } 25 private: 26 int j; 27 friend int main(); 28 } Ted; 29 friend int main(); 30 31 }; 32 33 34 int main() 35 36 { 37 A tt(100); 38 cout<<tt.i<<"\t"<<tt.Ted.j<<endl; 39 return 0; 40 41 } 42 ~ 補充說明(Supplement): -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 220.134.214.228 ※ 文章網址: http://www.ptt.cc/bbs/C_and_CPP/M.1418695999.A.C0B.html ※ 編輯: bethlehem (220.134.214.228), 12/16/2014 10:14:22 ※ 編輯: bethlehem (220.134.214.228), 12/16/2014 10:16:09 ※ 編輯: bethlehem (220.134.214.228), 12/16/2014 10:18:02 ※ 編輯: bethlehem (220.134.214.228), 12/16/2014 10:18:42 ※ 編輯: bethlehem (220.134.214.228), 12/16/2014 10:19:41

12/16 10:45, , 1F
寫 A(){ B Ted(ia); } 是宣告區域變數 Ted, 沒有動到 class
12/16 10:45, 1F

12/16 10:45, , 2F
member 的那個 Ted
12/16 10:45, 2F
文章代碼(AID): #1KZvK_mB (C_and_CPP)
文章代碼(AID): #1KZvK_mB (C_and_CPP)