dynamic_cast from derived to base : class cast « Class « C++ Tutorial






#include <iostream>
#include <typeinfo>

using namespace std;

class Base {
public:
   virtual ~Base( ) {} // Make this a polymorphic class
};
class Derived : public Base {
public:
   virtual ~Derived( ) {}
};

int main( ) {

   Derived d;

   // Query the type relationship
   if (dynamic_cast<Base*>(&d)) {
      cout << "Derived is a subclass of Base" << endl;
   }
   else {
      cout << "Derived is NOT a subclass of Base" << endl;
   }
}
Derived is a subclass of Base








9.21.class cast
9.21.1.dynamic_cast from derived to base
9.21.2.static_cast operator: cast base class pointer into derived class pointer
9.21.3.Demonstrating the const_cast operator.
9.21.4.dynamic_cast between base class and subclass