C++ c++;不使用默认构造函数的动态二维数组

C++ c++;不使用默认构造函数的动态二维数组,c++,arrays,dynamic,constructor,default,C++,Arrays,Dynamic,Constructor,Default,我遇到了一个我不知道的问题。我正在研究一个HGE项目,但这是一个C++问题,不是HGE引擎本身。 问题: 我想创建一个2D阵列,在5个不同的海龟身上有3个不同的动画。然而,我需要在HGE中使用类似这样的构造函数; turtleAnim[i]=新的hgeAnimation(turtleTexture,6,6,0,0,110,78) 但我不知道怎么做!interwebz上的所有示例都像处理默认构造函数一样处理问题。大概是这样的: hgAnimation* theSecondAnimOnTheFour

我遇到了一个我不知道的问题。我正在研究一个HGE项目,但这是一个C++问题,不是HGE引擎本身。 问题: 我想创建一个2D阵列,在5个不同的海龟身上有3个不同的动画。然而,我需要在HGE中使用类似这样的构造函数; turtleAnim[i]=新的hgeAnimation(turtleTexture,6,6,0,0,110,78)

但我不知道怎么做!interwebz上的所有示例都像处理默认构造函数一样处理问题。大概是这样的:

hgAnimation* theSecondAnimOnTheFourthTurtle = turtleAnim[4][2];
theSecondAnimOnTheFourthTurtle->DoSomething();
课堂上:

#define     TURTLECOUNT     5
#define     TURTLEANIMATIONS    3

private:
hgeAnimation** turtleAnim;
在.cpp中

turtleAnim= new hgeAnimation*[TURTLECOUNT];

for(int i=0; i<TURTLECOUNT; i++)
{
    turtleAnim[i]= new hgeAnimation[TURTLEANIMATIONS]; // Uses default constructor. I don't want that cuz it doesn't exists.
    turtleAnim[i]->Play();
}
turtleAnim=新hgeAnimation*[TURTLECOUNT];
对于(inti=0;iPlay();
}

首先,您必须决定是将对象放在堆栈上还是堆上。因为它们是对象,您可能希望它们放在堆上,这意味着您的2D阵列将有3颗星,您将访问如下动画:

hgAnimation* theSecondAnimOnTheFourthTurtle = turtleAnim[4][2];
theSecondAnimOnTheFourthTurtle->DoSomething();
如果这是你想要的,那么首先你要做一个指针矩阵

hgeAnimation*** turtleAnim = new hgeAnimation**[TURTLECOUNT];
然后你可以在海龟之间循环。对于每只海龟,你制作一个指向动画的指针数组。对于该数组的每个元素,你制作动画本身

for (int turt=0; turt<TURTLECOUNT; ++turt) {
   turtleAnim[turt] = new hgeAnimation*[TURTLEANIMATIONS];
   for (int ani=0; ani<TURTLEANIMATIONS; ++ani) {
      turtleAnim[turt][ani] = new hgeAnimation(parameter1, par2, par3);
   }
}
这样,您就可以在单个阵列中制作海龟:

ATurtle* turtles[TURTLECOUNT];
for (int t=0; t<TURTLECOUNT; ++t) {
    turtles[t] = new ATurtle(par1, par2);
}

std::vector::iterator i,end=myAnimations.end();
对于(i=myAnimations.begin();i!=end;++i){
(*i)->DoSomething();
}
不过,在本例中,您仍然必须对向量的每个元素调用delete,因为您为每个元素都调用了new

ATurtle::~ATurtle() {
   std::vector<hgAnimation*>::iterator i, end=myAnimations.end();
   for (i=myAnimations.begin(); i!=end; ++i) {
       delete (*i);
   }       
}
atartle::~atartle(){
std::vector::迭代器i,end=myAnimations.end();
对于(i=myAnimations.begin();i!=end;++i){
删除(*i);
}       
}

首先,您必须决定是将对象放在堆栈上还是堆上。因为它们是对象,您可能希望它们放在堆上,这意味着您的2D阵列将有3颗星,您将访问如下动画:

hgAnimation* theSecondAnimOnTheFourthTurtle = turtleAnim[4][2];
theSecondAnimOnTheFourthTurtle->DoSomething();
如果这是你想要的,那么首先你要做一个指针矩阵

hgeAnimation*** turtleAnim = new hgeAnimation**[TURTLECOUNT];
然后你可以在海龟之间循环。对于每只海龟,你制作一个指向动画的指针数组。对于该数组的每个元素,你制作动画本身

for (int turt=0; turt<TURTLECOUNT; ++turt) {
   turtleAnim[turt] = new hgeAnimation*[TURTLEANIMATIONS];
   for (int ani=0; ani<TURTLEANIMATIONS; ++ani) {
      turtleAnim[turt][ani] = new hgeAnimation(parameter1, par2, par3);
   }
}
这样,您就可以在单个阵列中制作海龟:

ATurtle* turtles[TURTLECOUNT];
for (int t=0; t<TURTLECOUNT; ++t) {
    turtles[t] = new ATurtle(par1, par2);
}

std::vector::iterator i,end=myAnimations.end();
对于(i=myAnimations.begin();i!=end;++i){
(*i)->DoSomething();
}
不过,在本例中,您仍然必须对向量的每个元素调用delete,因为您为每个元素都调用了new

ATurtle::~ATurtle() {
   std::vector<hgAnimation*>::iterator i, end=myAnimations.end();
   for (i=myAnimations.begin(); i!=end; ++i) {
       delete (*i);
   }       
}
atartle::~atartle(){
std::vector::迭代器i,end=myAnimations.end();
对于(i=myAnimations.begin();i!=end;++i){
删除(*i);
}       
}

那么std::vector呢?让您指定应该初始化哪些元素。它比原始数组更“现代”和面向对象。可能的副本您可以尝试更混乱的“hgeAnimation***turtleAnim;”并对主体进行相应的更改,但最终必须提供ctor参数。正如@DavidBrown所建议的,请看这里关于“placement new”的讨论:
std::vector
?它让您指定应该初始化哪些元素。它更“现代”和面向对象的,而不是原始数组。可能的副本您可以尝试更混乱的“hgeAnimation***turtleAnim;”并对主体进行相应的更改,但最终您必须提供ctor参数。正如@DavidBrown所建议的,请参阅此处关于“新放置”的讨论: