topical media & game development

talk show tell print

professional-program-12-Casts-DynamicCast.c

? / professional-program-12-Casts-DynamicCast.c


  include <typeinfo>
  include <iostream>
  using namespace std;
  
  class Base
  {
  public:
    Base() {};
    virtual ~Base() {}
  };
  
  class Derived : public Base
  {
  public:
    Derived() {}
    virtual ~Derived() {}
  };
  
  int main(int argc, char** argv)
  {
    Base* b;
    Derived* d = new Derived();
  
    b = d;
    d = dynamic_cast<Derived*>(b);
  
    Base base;
    Derived derived;
  
    Base& br = base;
  
    try {
      Derived& dr = dynamic_cast<Derived&>(br);
    } catch (bad_cast&) {
      cout << "Bad cast!\n";
    }
  
    return (0);
  }
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.