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

作者: peterwu4 (notd)   2017-12-01 20:41:50
※ 引述《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去做啦~

Links booklink

Contact Us: admin [ a t ] ucptt.com