title: C++ static_cast vs dynamic_cast
body: <pre><code>#include &lt;iostream&gt;
  
  class Base {
  };
  
  class Derived : public Base {
  };
  
  int main() {
      Base* bptr = new Derived();
  
      Derived* dptr = static_cast&lt;Derived*&gt;(bptr);
  
      std::cout &lt;&lt; dptr &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>In the above code, I should be using dynamic_cast but am using
  static_cast incorrectly. static_cast should never be used while
  downcasting, since compiler has no way to determine we are doing
  cast correctly.</p>
  
  <p>Why isn't this a compile error ? I am using gcc 4.9.2. Do you guys
  think this is a bug in the compiler, or does C++ standards allow such code ?</p>
  
  <p>Edit: Thanks for the responses. Actually, Base class above is polymorphic
  i.e. it has virtual functions, but I left them out for this example. I should
  have thrown in a virtual destructor in the Base to make it clear that it
  was polymorphic - my bad.</p>