title: dynamic_cast to derived type fails, Why?
body: <p>I'd like to have some help on an issue I face.</p>
  
  <p>I'm using serialization (TinyXml to be precise) to save objects in a file and I have a problem on deserialization.</p>
  
  <p>My objects contain pointers, which are not saved directly but instead the ID of the object pointed to is saved.</p>
  
  <p>Every object I save derivate ASerializable, directly or not.</p>
  
  <h2>Here is my hierarchy :</h2>
  
  <pre><code>//Holds pure virtual method: TiXmlElement * ToXml() = 0;
  class ISerializable;
  
  // Basic implementation of ISerializable, this class has a unique ID.
  class ASerializable : public ISerializable;
  
  // For security sake, this class is not real, just a demonstration of my problem.
  // Overrides TiXmlElement * ToXml()
  class Base : public ASerializable;
  
  // Overrides TiXmlElement * ToXml()
  class Derived : public Base;
  </code></pre>
  
  <p>Upon deserialization, I add each object I create into a static map(ID, ASerializable*), which holds the data until dereference.</p>
  
  <h2>How I dereference objects</h2>
  
  <p>I use a template class which holds a T**, which is the pointer to dereference once all the deserialization is done.</p>
  
  <pre><code>template&lt;typename T&gt;
  class Reference&lt;T&gt; {
      T** ref_ptr;
      int id; // This is the ID of the referenced object
      void Dereference();
  }
  </code></pre>
  
  <p>Basically, <code>Reference&lt;T&gt;::Dereference()</code> gets the object in the map, then cast it to the good type (which is <code>T*</code>) and change the value of <code>*ref_ptr</code> to the cast object.</p>
  
  <p>I'm using <code>dynamic_cast&lt;T*&gt;( objects_map[id] )</code> to cast my object and for some reason, it fails everytime.</p>
  
  <p><strong>I can't cast from a <code>ASerialisable*</code> to a <code>Derived*</code> without losing the data of <code>Derived</code>.</strong></p>
  
  <p>Here is the pattern I want to achieve :</p>
  
  <ol>
  <li><p>On deserialized, the item add itself to the objects map;</p></li>
  <li><p>If the object is a reference, add a Reference (with the good type, I know it) to a Stack</p></li>
  <li><p>Once all deserialization is done, unstack all references and calls <code>Dereference()</code></p></li>
  <li><p>Be really happy.</p></li>
  </ol>
  
  <p>Thank you soooo dor your time and precious help, really appreciated.</p>
  
  <p>{enjoy}
  EDIT : 
  Okay, I'll try to be clearer, but it is a bit complex.</p>
  
  <p>When I parse my XML file back, there are sometimes element called <code>&lt;reference id="X"&gt;</code> : those are reference to another instance (<code>ASerializable</code>);</p>
  
  <p>When I encounter a <code>&lt;reference&gt;</code> object, I add it to a stack, to be dereferenced later.
  When I encounter a specific object, like a <code>&lt;Base id="X" ... data ... /&gt;</code> I build it, then I add it to a object map, as an <code>ASerializable*</code>;</p>
  
  <p>Once I finished building all my objects, I unstack each of the reference and call <code>Dereference()</code>, that SHOULD go get the REAL INSTANCE of the object, cast it to the good type using <code>dynamic_cast</code> then change the value of his ref_ptr to point that object.</p>
  
  <p>Don't hesitate to ask for other questions if needed.</p>