Re: [問題] Object array在class裡的宣告方法

看板C_and_CPP作者 (notd)時間8年前 (2017/12/01 20:41), 8年前編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/3 (看更多)
※ 引述《birka1222 (筱望)》之銘言: : 如題 : 有一個class叫staff,constructor 需輸入一個int代表編號。 : 現在有另一個class叫team,constructor不須傳入任何值 : team裡有一個staff的array,ST[100],對應編號1-100。 : 請問要怎麼寫team的constructor? : 像這樣的感覺: : class staff{ : public: : staff(int a){b=a;} : private: : int b;} : class team{ : public: : team(){}//這裡怎麼寫? : private: : staff ST[100];} 為了完成你的心願XD ============= class Staff { public: Staff() = default; Staff(int a) : a(a) {} private: int a; }; class Team { public: Team() { for (int i = 0; i < num; i++) { staffs[i] = Staff(i); } } private: static constexpr int num = 100; Staff staffs[num]; }; int main() { Team team; return 0; } =============== 大概是這樣~ 一般都會用std::vector去做啦~ -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.224.126.9 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1512132113.A.CB2.html ※ 編輯: peterwu4 (36.224.126.9), 12/01/2017 20:46:12 ※ 編輯: peterwu4 (36.224.126.9), 12/02/2017 00:15:44
文章代碼(AID): #1Q8KuHoo (C_and_CPP)
文章代碼(AID): #1Q8KuHoo (C_and_CPP)