// ========================================================================== // $Id: dynamic_cast.cpp,v 1.1 2011/10/19 03:24:40 jlang Exp $ // CSI2372 example Code for lecture 7 // ========================================================================== // (C)opyright: // // Jochen Lang // EECS, University of Ottawa // 800 King Edward Ave. // Ottawa, On., K1N 6N5 // Canada. // http://www.eecs.uottawa.ca // // Creator: jlang (Jochen Lang) // Email: jlang@eecs.uottawa.ca // ========================================================================== // $Log: dynamic_cast.cpp,v $ // Revision 1.1 2011/10/19 03:24:40 jlang // Added code for lecture 7 // // // ========================================================================== #include #include "bounds.h" #include "circle.h" #include "aa_box.h" #include "obbox.h" using std::cout; using std::endl; Bounds *getBounds() { return new Circle(); } Bounds& getBoundsAsRef() { AABox *res = new AABox(); return *res; } int main() { // old-style casts are evil OBBox *myNotObb = (OBBox *) getBounds(); myNotObb->print(); cout << endl; // myObb will be 0 OBBox *myObb = dynamic_cast(getBounds()); if ( myObb == 0 ) { // Don't forget to check! cout << "Bounds was not a OBB!" << endl; } // Line below will throw OBBox &myObbRef = dynamic_cast( getBoundsAsRef() ); return 0; }