这是问题:
如何正确地做 IT?
IT = 动态添加对象(意味着创建类结构来支持它)
class Branch
{
Leaves lv; //it should have many leaves!!
}
class Tree
{
Branch br; //it should have many branchs!!!
}
现在是一个无效的例子(也不是 c++!!,但我试着画出这个想法)
class Branch
{
static lv_count;
Leaves lv; //it should have many leaves!! (and should be some pointer)
public:
add(Leave lv)
{
lv[lv_count] = lv;
lv_count ++ ;
}
}
class Tree
{
static br_count;
Branch br; //it should have many branchs!!! (and should be some pointer)
Tree
public:
add(Branch br)
{
br[br_count] = lv;
br_count ++ ;
}
}
这是例如,到达一个愚蠢的方法:
class Branch
{
static count;
Leaves l[1000]; //mmm i don't like this
//...
}
class Tree
{
static count;
Branch b[1000]; //mmm i don't like this
//...
}
我想知道执行此操作的正式正常方法,谢谢!!!!!!
最佳答案
std::vector 是你正在寻找的东西,我猜......
class Tree
{
std::vector<Branch> branches;
};
关于c++ - 如何动态添加对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1947014/