title: dynamic cast to non-derived object in order to call a function works?
body: <p>I read in a C++ book that you can use dynamic_cast to downcast a pointer to a base object to a derived object pointer, if the object it points to actually is that of the derived type. The format is <code>dynamic_cast&lt;Derived*&gt;(basePointer)</code>. Furthermore, I read on this website that dynamic_cast should return a null pointer if the object it points to cannot be converted to the derived class type.</p>
  
  <p>However, I recently tried having a pointer to a plain object with a virtual function downcast to an object that's derived from a different class, in order to call one of that class' functions. To my surprise, it worked:</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  class Object {
      public:
          virtual ~Object () {}
  };
  
  class Base {
      public:
          virtual void doSomething () {
              cout &lt;&lt; "Done something!" &lt;&lt; endl;
          }
  };
  
  class Derived: public Base {
      public:
          virtual void doSomething () {
              cout &lt;&lt; "You done goofed!" &lt;&lt; endl;
          }
          void printFoo () {
              cout &lt;&lt; "Foo" &lt;&lt; endl;
          }
  
      private:
          int x;
  };
  
  int main () {
      Object o;
      Object* p = &amp;o;
  
      if(dynamic_cast&lt;Derived*&gt;(p))
          cout &lt;&lt; "Yep, baby is derived!" &lt;&lt; endl;
      else
          cout &lt;&lt; "Isn't derived." &lt;&lt; endl;
  
      dynamic_cast&lt;Derived*&gt;(p)-&gt;printFoo();
  
  
      return 0;
  }
  </code></pre>
  
  <p>Here, Base is a base class type designed to be used polymorphically (that is, it contains a virtual function), and Derived is a class type derived from Base. Object is just a plain class that is not related to either class, but its destructor is made virtual so that it can be used polymorphically. I'll explain the purpose of Derived::x in a moment. In the main function, an Object is created and a pointer to Object is assigned to its address. It checks whether Derived is a descendant of that pointer, and prints whether it is or it isn't. Then it casts that pointer to a pointer of type Derived, and Derived's printFoo function is called.</p>
  
  <p>This shouldn't work, but it does. When I run it, it displays "Isn't derived," so it clearly understands that Obect is not Derived from Base, but then it prints "Foo" on the screen and exits without error. However, if I add the line <code>x = 1;</code> to the printFoo function, it exits with a Segmentation fault, as it's trying to assign a variable which doesn't exist in that object. Furthermore, this only seems to work with non-virtual functions. If I try to call the virtual function doSomething, for instance, I get the Segmentation fault before "You done goofed!" is ever printed out.</p>
  
  <p>What's going on here? Shouldn't <code>dynamic_cast&lt;Derived*&gt;(p)</code> return a null pointer, so that trying to call printFoo from that would automatically cause an error? Why is that working when it shouldn't?</p>