home *** CD-ROM | disk | FTP | other *** search
/ Using Visual C++ 4 (Special Edition) / Using_Visual_C_4_Special_Edition_QUE_1996.iso / ch18 / dyncast2.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-13  |  679 b   |  32 lines

  1. #include <iostream.h>
  2.  
  3. class Base {
  4.     // Do nothing (not a polymorphic class)
  5.     void virtual Nothing() { } // Now polymorphic
  6. };
  7.  
  8. class Derived : public Base {
  9.     // Do nothing (not a polymorphic class)
  10. };
  11.  
  12. void main()
  13. {
  14.     Derived  MyDerived;
  15.     Derived* pMyDerived = &MyDerived;
  16.  
  17.     // Successful upcast
  18.     Base* pBaseTest = dynamic_cast<Base*>(pMyDerived);
  19.     cout << "pMyDerived ";
  20.     cout << (pBaseTest ? "is" : "is not");
  21.     cout << " a Base*.\n";
  22.  
  23.     // Successful(??) downcast
  24.     if (pBaseTest) {
  25.         Derived* pDerivedTest =
  26.             dynamic_cast<Derived*>(pBaseTest);
  27.         cout << "pBaseTest ";
  28.         cout << (pDerivedTest ? "is" : "is not");
  29.         cout << " a Derived*.\n";
  30.     }
  31. }
  32.