简体   繁体   English

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

[英]c++ Dynamic 2d array not using default constructor

I got a problem that is beyond my knowledge. 我遇到了我无法理解的问题。 I'm working on a HGE project, but this is a c++ issue, not the HGE engine itself. 我正在开发一个HGE项目,但这是一个c ++问题,而不是HGE引擎本身。

The question: I want to create a 2D array with 3 different animations on 5 different turtles. 问题:我想在5个不同的乌龟上创建具有3个不同动画的2D数组。 However, I need to use a constructor in HGE something like this; 但是,我需要在HGE中使用类似以下的构造函数: turtleAnim[i] = new hgeAnimation( turtleTexture, 6, 6, 0, 0, 110, 78 ) turtleAnim [i] = new hgeAnimation(turtleTexture,6,6,0,0,110,78)

But I don't know how to do it! 但是我不知道该怎么做! All examples on the interwebz handle the problem as if it got a default constructor. interwebz上的所有示例都像处理默认构造函数一样处理该问题。 Something like this: 像这样:

in class: 在班上:

#define     TURTLECOUNT     5
#define     TURTLEANIMATIONS    3

private:
hgeAnimation** turtleAnim;

in the.cpp 在.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();
}

First, you have to decide whether you want your objects on the stack or the heap. 首先,您必须决定是将对象放在堆栈还是堆上。 Since they're objects, you probably want them on the heap, which means your 2D array will have 3 stars, and you will access the animations like this: 由于它们是对象,因此您可能希望将它们放在堆中,这意味着您的2D数组将具有3星,并且您将像这样访问动画:

hgAnimation* theSecondAnimOnTheFourthTurtle = turtleAnim[4][2];
theSecondAnimOnTheFourthTurtle->DoSomething();

If that's what you want, then first you make the matrix of pointers. 如果那是您想要的,那么首先创建指针矩阵。

hgeAnimation*** turtleAnim = new hgeAnimation**[TURTLECOUNT];

Then you can loop through the turtles. 然后,您可以在海龟中循环。 For each turtle, you make an array of pointers to the animations. 对于每只海龟,您都要制作一个指向动画的指针数组。 For each element of that array, you make the animation itself. 对于该数组的每个元素,您自己制作动画。

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);
   }
}

If that looks tricky, deleting all the arrays will be a pain too: 如果这看起来很棘手,那么删除所有数组也会很麻烦:

for (int turt=0; turt<TURTLECOUNT; ++turt) {
   for (int ani=0; ani<TURTLEANIMATIONS; ++ani) {
      delete turtleAnim[turt][ani];
   }
   delete[] turtleAnim[turt];
}
delete[] turtleAnim;

The fact that this is tricky is a good sign that there's probably a more simple way to design it. 这很棘手的事实表明,可能存在更简单的设计方法。

How about a turtle class that has a member like: 有成员的乌龟类怎么样:

class ATurtle {
  private:
    std::vector<hgeAnimation*>   myAnimations;

Then in your class's constructor, you can do whatever you want in order to make the animations. 然后,在您的类的构造函数中,您可以做任何想做的动画。

ATurtle::ATurtle(par1, par2, par3) {
    myAnimations.push_back( new hgeAnimation(par1, x, y) );
    myAnimations.push_back( new hgeAnimation(par2, z, a) );
    myAnimations.push_back( new hgeAnimation(par3, b, c) );
}

That way, you can make your turtles in a single array: 这样,您可以将海龟分成一个阵列:

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

And in your turtle class, you would access the animations like so: 在乌龟类中,您将像这样访问动画:

(*(myAnimations.at(1)))->DoSomething();

or 要么

std::vector<hgAnimation*>::iterator i, end=myAnimations.end();
for (i=myAnimations.begin(); i!=end; ++i) {
    (*i)->DoSomething();
}

You will still have to call delete on each element of your vector in this case, though, since you called new for every element. 但是,在这种情况下,由于对每个元素都调用了new,因此仍然必须在vector的每个元素上调用delete。

ATurtle::~ATurtle() {
   std::vector<hgAnimation*>::iterator i, end=myAnimations.end();
   for (i=myAnimations.begin(); i!=end; ++i) {
       delete (*i);
   }       
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM