title: Call a function of base class casted to derived class
body: <p>Say that I have the following code, is it safe to use?</p>
  
  <p>Base class:</p>
  
  <pre><code>class B
  {
      public:
      B(bool isDerived = false) : m_isDerived(isDerived) {}
      bool isDerived() { return m_isDerived; }
  
      private:
      bool m_isDerived;
  }
  </code></pre>
  
  <p>Derived class:</p>
  
  <pre><code>class D : public B
  {
      public:
      D() : B(true) {}
  }
  </code></pre>
  
  <p>Code:</p>
  
  <pre><code>B* b = new B(); // Create new base class
  D* unknown = static_cast&lt;D*&gt;(b); // Cast the base class into a derived class
  
  if (unknown-&gt;isDerived()) // Is this allowed?
      // unknown is a D and can be used as such
  else
      // unknown is not a D and can not be used
  </code></pre>
  
  <p>Can I safely call unknown->isDerived() even though unknown is really a B in this case?</p>
  
  <p>We make the assumption that unknown NEVER contains anything other than a B* or D* and that we NEVER do anything with unknown until isDerived() have been checked on it.</p>
  
  <p><strong>Edit</strong>:</p>
  
  <p><em>Given the questions I will try to explain the reason why I'm trying to do this:
  So essentially I have a Windows tree control which of course can't be directly connected to the c++ tree structure I'm using to store my data. So I have to reinterpret_cast my data to a DWORD_PTR that is stored together with each node in the tree control so I have a connection between the two. My tree structure consists of either the base type (a normal node) or the derived type (a node with more info in it that should be handled differently). The pointers to these are reinterpret_cast:ed and put in the tree control.</em></p>
  
  <p><em>Now, when I'm stepping through the tree control I want to act on the nodes which are of the derived type, so I want to reinterpret_cast the DWORD_PTR into a derived type. But to be entirely correct I should reinterpret_cast it to a base type first (I guess?), and then downcast it to the derived type if it is a derived type. However I thought I could make it a bit simpler by reinterpret_cast it into a derived type immediately and then check via the function if it really is a derived type. If it wasn't I do nothing more with it. In my mind the base class data in the pointer should be at the same memory location no matter how much derived it is, but I might be wrong which is why I ask here.</em></p>
  
  <p>I wanted to make the question clearer by not involving Windows in it, but what I want in reality would be something closer to this:</p>
  
  <pre><code>B* b1 = new B();
  B* b2 = new D();
  DWORD_PTR ptr1 = reinterpret_cast&lt;DWORD_PTR&gt;(b1);
  DWORD_PTR ptr2 = reinterpret_cast&lt;DWORD_PTR&gt;(b2);
  
  D* unknown = reinterpret_cast&lt;D*&gt;(ptr1 /* or ptr2 */); // Safe?
  unknown-&gt;isDerived(); // Safe?
  </code></pre>
  
  <p>Essentially no matter what I do it's still unsafe at some level as I must reinterpret_cast the data.</p>