Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To solve the problem which you have mentioned, although you are going in right direction, but you are doing it the wrong way. This is what you would need to do</p> <ul> <li>Define a base class (which you are already doing) with virtual functions which would be overridden by derived classes <code>Enemy</code> and <code>Civilian</code> in your case.</li> <li>You need to choose a proper container with will store your object. You have taken a <code>std::vector&lt;IActor&gt;</code> which is not a good choice because <ul> <li>Firstly when you are adding objects to the vector it is leading to object slicing. This means that only the <code>IActor</code> part of <code>Enemy</code> or <code>Civilian</code> is being stored instead of the whole object.</li> <li>Secondly you need to call functions depending on the type of the object (<code>virtual functions</code>), which can only happen if you use pointers. </li> </ul></li> </ul> <p>Both of the reason above point to the fact that you need to use a container which can contain pointers, something like <code>std::vector&lt;IActor*&gt;</code> . But a better choice would be to use <code>container of smart pointers</code> which will save you from memory management headaches. You can use any of the smart pointers depending upon your need (but not <code>auto_ptr</code>)</p> <p>This is what your code would look like</p> <pre><code>// somewhere during init std::vector&lt;some_smart_ptr&lt;IActor&gt; &gt; ActorList; ActorList.push_back(some_smart_ptr(new Enemy())); ActorList.push_back(some_smart_ptr(new Civilian())); </code></pre> <p>and</p> <pre><code>// main loop while(!done) { BOOST_FOREACH(some_smart_ptr&lt;IActor&gt; CurrentActor, ActorList) { CurrentActor-&gt;Update(); CurrentActor-&gt;Draw(); } } </code></pre> <p>Which is pretty much similar to your original code except for smart pointers part</p>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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