-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrytemplate.cpp
61 lines (50 loc) · 1.08 KB
/
trytemplate.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include<iostream>
using std::cout; using std::endl;
class A {
public:
virtual ~A() = default;
};
class B : virtual public A{
};
class C : virtual public A {};
class D : public B, public C {
};
class E : public D {
};
class F : public E{};
template<class T>
void Fun( T& ref){
B* p = &ref;
try{throw ref;}
catch(E){cout << "E "; }
catch(D){cout << "D ";}
catch(B){cout << "B ";}
catch(A){cout << "A ";}
catch(C){cout << "C ";}
catch(...){cout << "Gen ";}
}
int main(){
A a; B b;
C c;
D d;
E e; F f;
A* pa = &b;
B* pb = dynamic_cast<B*>(pa);
D* pd = &f;
E* pe = static_cast<E*>(pd);
C* pc = dynamic_cast<E*>(pd);
//Fun(a); cout << endl;
Fun(b); cout << endl;
//Fun(c); cout << endl;
Fun(d); cout << endl;
Fun(e); cout << endl;
Fun(f); cout << endl;
//Fun(*pa); cout << endl;
Fun(*pb); cout << endl;
//Fun(*pc); cout << endl;
Fun(*pd); cout << endl;
Fun(*pe); cout << endl;
Fun<B>(*pd); cout << endl;
Fun<D>(*pd); cout << endl;
//Fun<E>(*pd); cout << endl;
}