Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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:</p> <pre><code>hgAnimation* theSecondAnimOnTheFourthTurtle = turtleAnim[4][2]; theSecondAnimOnTheFourthTurtle-&gt;DoSomething(); </code></pre> <p>If that's what you want, then first you make the matrix of pointers.</p> <pre><code>hgeAnimation*** turtleAnim = new hgeAnimation**[TURTLECOUNT]; </code></pre> <p>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.</p> <pre><code>for (int turt=0; turt&lt;TURTLECOUNT; ++turt) { turtleAnim[turt] = new hgeAnimation*[TURTLEANIMATIONS]; for (int ani=0; ani&lt;TURTLEANIMATIONS; ++ani) { turtleAnim[turt][ani] = new hgeAnimation(parameter1, par2, par3); } } </code></pre> <p>If that looks tricky, deleting all the arrays will be a pain too:</p> <pre><code>for (int turt=0; turt&lt;TURTLECOUNT; ++turt) { for (int ani=0; ani&lt;TURTLEANIMATIONS; ++ani) { delete turtleAnim[turt][ani]; } delete[] turtleAnim[turt]; } delete[] turtleAnim; </code></pre> <p>The fact that this is tricky is a good sign that there's probably a more simple way to design it.</p> <p>How about a turtle class that has a member like:</p> <pre><code>class ATurtle { private: std::vector&lt;hgeAnimation*&gt; myAnimations; </code></pre> <p>Then in your class's constructor, you can do whatever you want in order to make the animations.</p> <pre><code>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) ); } </code></pre> <p>That way, you can make your turtles in a single array:</p> <pre><code>ATurtle* turtles[TURTLECOUNT]; for (int t=0; t&lt;TURTLECOUNT; ++t) { turtles[t] = new ATurtle(par1, par2); } </code></pre> <p>And in your turtle class, you would access the animations like so:</p> <pre><code>(*(myAnimations.at(1)))-&gt;DoSomething(); </code></pre> <p>or</p> <pre><code>std::vector&lt;hgAnimation*&gt;::iterator i, end=myAnimations.end(); for (i=myAnimations.begin(); i!=end; ++i) { (*i)-&gt;DoSomething(); } </code></pre> <p>You will still have to call delete on each element of your vector in this case, though, since you called new for every element.</p> <pre><code>ATurtle::~ATurtle() { std::vector&lt;hgAnimation*&gt;::iterator i, end=myAnimations.end(); for (i=myAnimations.begin(); i!=end; ++i) { delete (*i); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload