#include <iostream>
#include <typeinfo>

struct Base
{
  virtual void test() { /* ... */ }
};
struct Derived : Base
{
  virtual void test() { /* ... */ }
};

int main()
{
  Base *bp = new Base;

  // null if dynamic_cast is invalid
  if (Derived *dp = dynamic_cast<Derived*>(bp) )
  {
    std::cout << "Dynamic cast with pointer succeeded" << std::endl;
  }
  else
  {
    std::cout << "Dynamic cast with pointer failed" << std::endl;
  }


  Base b;
  Base &br = b;

  // throws exception if dynamic_cast is invalid
  try
  {
    Derived &dr = dynamic_cast<Derived&>(br);
    std::cout << "Dynamic cast with reference succeeded" << std::endl;
  }
  catch(std::bad_cast)
  {
    std::cout << "Dynamic cast with reference failed" << std::endl;
  }
}