-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcast.cpp
More file actions
54 lines (45 loc) · 1.4 KB
/
cast.cpp
File metadata and controls
54 lines (45 loc) · 1.4 KB
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
/**
* http://www.cplusplus.com/doc/tutorial/typecasting/
*/
#include <iostream>
using namespace std;
class A {
public:
int a;
virtual void dummp() {} // first mistake, virtual function is necessary for polymorphic
};
class B : public A { // second mistake inherit public
int b;
};
/*
int a = 7;
double* p1 = (double*) &a; // ok (but a is not a double)
double* p2 = static_cast<double*>(&a); // error
double* p2 = reinterpret_cast<double*>(&a); // ok: I really mean it
const int c = 7;
int* q1 = &c; // error
int* q2 = (int*)&c; // ok (but *q2=2; is still invalid code and may fail)
int* q3 = static_cast<int*>(&c); // error: static_cast doesn't cast away const
int* q4 = const_cast<int*>(&c); // I really mean it
*/
int main() {
A a;
B b;
try {
A *a = new A;
A *b = new B;
// if no virtual function defined in A
// error: cannot dynamic_cast ‘a’ (of type ‘class A*’) to type
// ‘class B*’ (source type is not polymorphic)
B *c;
c = dynamic_cast<B*>(a); // run time check to make sure completeness of object
if (!c) cout << "null pointer on a->c" << endl;
c = dynamic_cast<B*>(b);
if (!c) cout << "null pointer on b->c" << endl;
else cout << "succeed in casting b->c" << endl;
}
catch (exception& e) {
cout << "Exception: " << e.what();
}
return 1;
}