title: Casting pointer from base type to child type
body: <p>I'm building a simple game design for a project of mine. I have the following classes: </p>
  
  <pre><code>class Character
  {
  public: 
     virtual void Display();
     virtual void SetParameters( char* param, ... );
  };
  
  class NonPlayableCharacter : public Character
  {
  
  public:
     virtual void Display();
     virtual void SetParameters( char* paaram, ... );
     int GetNPCState();
  }
  </code></pre>
  
  <p>And then I have a bunch of classes that derive either from Character or NonPlayableCharacter. I define it like so:</p>
  
  <pre><code>std::vector&lt;Character*&gt; _allChar;
  </code></pre>
  
  <p>My problem is that at any given time I would want to perform some operation on one of the element of the vector. So getting an element out of the vector I can't directly call the method <code>GetNPCState()</code> because the element in the vector are of Character* type. So doing this: </p>
  
  <pre><code>_allChar[0]-&gt;GetNPCState();
  </code></pre>
  
  <p>doesn't work. So I tried doing it with the famous dynamic_cast:</p>
  
  <pre><code>NonPlayableCharacter* test = dynamic_cast&lt;NonPlayableCharacter*&gt;(_allChar[0]);
  test-&gt;GetNPCState();
  </code></pre>
  
  <p>The problem with this last attempt is that <code>GetNPCState()</code> crashes because the object is null, and I know for a fact (via debugging) that _allChar[0] isn't null. </p>