Yurttas/PL/OOL/Cplusplus/F/02/06/04/00/exception 01.cpp

From ZCubes Wiki
Jump to navigation Jump to search
 1/*
 2   Copyright(C) 2000
 3   All Rights Reserved. Salih Yurttas, ZCubes, BitsOfCode Software Systems, Inc..
 4
 5   Permission to use, copy, modify, and distribute this
 6   software and its documentation for EDUCATIONAL purposes
 7   and without fee is hereby granted provided that this
 8   copyright notice appears in all copies.
 9
10   date   : January 1, 2000.
11   author : Salih Yurttas.
12
13   exception_01.cpp
14*/
15
16
17#include <iostream>
18
19#include <typeinfo>
20
21using namespace std;
22
23class A { 
24public: 
25  virtual ~A(){};
26};
27
28class B {
29public: 
30  virtual ~B(){};
31};
32
33class D : public B {};
34
35int main(int argc, char* argv[]) {
36
37  D d;
38
39  B& b = d; 
40
41  try {
42    A& ar = dynamic_cast<A&>(b); // need reference, no pointers!
43  }
44  catch(bad_cast e) {
45    cout << "Exception : " << e.what() << endl; 
46    cout << "dynamic_cast<A&>(b) failed" << endl;
47  }
48
49}