title: Virtual Functions: Iterating over a vector<Base Class> that is populated with subclass objects
body: <p>Short Description:<br>
  I am iterating over a vector calling a virtual function on every object in the vector in order to execute a sequence of actions.  The vector is of the base class as is the iterator.  All the objects are children.  When the virtual function is called it executes the base class's function.</p>
  
  <p>(Really) Long Description:
  I am attempting to model a creature that has a set of behaviors.  My base class is abstract with only two functions (virtual) which all the subclasses have overridden:</p>
  
  <pre><code>class Behavior
  {
  public:
       Behavior();
      ~Behavior(void){}
   virtual void execute(){} 
   virtual BEHAVIOR_TYPE getType() {return m_Type;}
  
  
  protected:
  BEHAVIOR_TYPE m_Type;
  };
  </code></pre>
  
  <p>I have created a number of children behaviors, such as move, consume, scout, etc.</p>
  
  <pre><code>class Move :
      public Behavior
  {
  public:
  BEHAVIOR_TYPE getType() {return m_Type;}
      enum Direction {N, NE, E, SE, S, SW, W, NW};
  Move(DOCO * d);
  ~Move(void);
  void execute() ;
      Direction chooseDirection();
      void setDirection(Direction newDirection);
  private:
      Direction m_Direction;
      DOCO *I;
  BEHAVIOR_TYPE m_Type;
  
  };
  </code></pre>
  
  <p>I created a vector onto which I pushed instances of each Behavior subclass as well as an iterator to traverse it:</p>
  
  <pre><code>vector&lt;Behavior&gt; m_Behavior;
  vector&lt;Behavior&gt;::iterator bIt;
  </code></pre>
  
  <p>When the creature gets an action sequence, I try to iterate over the vector, dereference the iterator, and call the execute function:</p>
  
  <pre><code>void World::justDoIt()
  {
      for(dIt=myDOCO.begin(); dIt!=myDOCO.end(); ++dIt)
  {
      vector&lt;Behavior&gt;::iterator myBehavior=(dIt)-&gt;getFirstBehavior();
      vector&lt;Behavior&gt;::iterator end=(dIt)-&gt;getLastBehavior();
      for(myBehavior; myBehavior!=end; ++myBehavior)
  
          (*myBehavior).execute();
  }
  }
  </code></pre>
  
  <p>The problem is that it executes the parent's function rather than the child's function.  </p>
  
  <p>In my understanding of late binding it should automatically call the appropriate function based on the type of object that was calling it rather than the type of pointer with which it was called, and in my code I anticipated it would be pointing to the child object.</p>
  
  <p>Obviously I have made an error and somehow told the program that I want these to be treated as parents instead of children, but I cannot find my error.  </p>
  
  <p>A second symptom is that it will not let me make the parents function pure virtual, because it says it cannot instantiate an abstract class.  I am not instantiating it explicitly anywhere in my code, but there must be somewhere I am doing it implicitly.  I cannot find where, however.  Certainly creating a vector to hold objects of the parent class does not require instantiating the parent, and that is the only time I reference the parent class directly.</p>
  
  <p>Any help would be greatly appreciated.</p>