pickzy.com

C  |  C++  |  Objective-C  |  VC++  |  Win32  |  MFC  |  Java  |  Php  |  Delphi  |  Visual Basic  |  .Net  |  Networking  |  General  |  Games  |  Jobs  |  Javascript  |  




Menu

pickSourcecode.com


        

 




 

Cpp > Programs

 

Cpp - Dynamic Casting

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


class Base 


 virtual 
void dummy() {} 
};

class Derived: public Base 


int a; 
};


int main () 
{

  try 

  {

    Base * bptrD = new Derived;

    Base * bptrB = new Base;

    Derived * dptr;


    dptr = dynamic_cast<Derived*>(bptrD);

    
if (dptr==0) cout << "Null pointer on first type-cast" << endl;

    dptr = dynamic_cast<Derived*>(bptrB);

    
if (dptr==0) cout << "Null pointer on second type-cast" << endl;
  } 

 catch (exception& e) 

 {

  cout << "Exception: " << e.what();

 }

  return 0;

}

 
Privacy Policy | About Us