-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path09_12_2008.cpp
64 lines (56 loc) · 1.23 KB
/
09_12_2008.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
62
63
64
#include<iostream>
using namespace std;
class D;
class B{
public: virtual D* f() =0;
};
class C{
public:
virtual C* g();
virtual B* h() =0;
};
class D: public B, public C{
public:
D* f() {cout << "D::f"; return new D;}
D* h() {cout << "D::h"; return dynamic_cast<D*>(g());}
};
C* C::g() {
cout << "C::g";
B* p = dynamic_cast<B*>(this);
if(p) return p->f();
else return this;
}
class E: public D{
public:
E* f() {
cout << "E::f";
E* p = dynamic_cast<E*>(g());
if(p) return p;
else return this;
}
};
class F: public E{
public:
E* g() {
cout << "F::g"; return new F;
}
E* h(){
cout <<"F::h";
E* p = dynamic_cast<E*>(E::g());
if(p) return p;
else return new F;
}
};
B* p; C* q; D* r;
int main(){
//p = new E; p->h();
//p = new E; p->f();ER
p = new D; (dynamic_cast<D*>(p))->h(); cout << endl;
q = new D; q->g(); cout << endl;
//q = new E; q->h();ER
//q = new E; q->g();ER
//r = new E; r->f();ER
r = new F; r->f(); cout << endl;
r = new F; r->g(); cout << endl;
r = new F; r->h();
}