作者peterwu4 (notd)
看板C_and_CPP
标题Re: [问题] Object array在class里的宣告方法
时间Fri Dec 1 20:41:50 2017
※ 引述《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://webptt.com/cn.aspx?n=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