dynamic_cast between base class and derived class : dynamic_cast « Class « C++






dynamic_cast between base class and derived class

  
#include <iostream>
#include <exception>
using namespace std;

class CBase {  virtual void dummy() {cout << "base";} };
class CDerived: public CBase { int a; };

int main () {
  try {
    CBase * pba = new CDerived;
    CBase * pbb = new CBase;
    CDerived * pd;

    pd = dynamic_cast<CDerived*>(pba);
    if (pd==0) cout << "Null pointer on first type-cast" << endl;

    pd = dynamic_cast<CDerived*>(pbb);
    if (pd==0) cout << "Null pointer on second type-cast" << endl;

  } catch (exception& e) {cout << "Exception: " << e.what();}
  return 0;
}
  
    
  








Related examples in the same category