-
Notifications
You must be signed in to change notification settings - Fork 0
/
cast.cpp
120 lines (101 loc) · 1.44 KB
/
cast.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//C++ Ç¿ÖÆÀàÐÍת»»
#include <vector>
#include <string>
#include <iostream>
#include <iterator>
using namespace std;
class A
{
public:
void show()
{
cout << "from A" << endl;
}
};
class B :public A
{
public:
void show()
{
cout << "from B" << endl;
}
};
void test_static_cast()
{
A *pa = new A();
pa->show();
B *pb = static_cast<B*>(pa);
pa->show();
B *ppb = new B();
ppb->show();
A *ppa = static_cast<A*>(ppb);
ppb->show();
}
class Employee
{
public:
Employee(){}
Employee(string inputname):name(inputname)
{
}
virtual void show()
{
cout << "from Employee "<<name << endl;
}
private:
string name;
};
class Programmer:public Employee
{
public:
Programmer()
{
};
Programmer(string inputname) :name(inputname)
{
}
void show()
{
cout << "from Programmer " << name << endl;
}
private:
string name;
};
void test_dynamic_cast()
{
Employee *pe=new Employee("nanning");
pe->show();
Programmer *pm = dynamic_cast<Programmer *>(pe);
//pm->show();
}
void test_reinterpret_cast()
{
int i=1024;
char *p = "Thisisanexample.";
i = reinterpret_cast<int>(p);
cout << i << endl;
cout << p << endl;
}
class BBB
{
public:
int m_iNum;
void show()
{
cout << m_iNum << endl;
}
};
void test_const_cast()
{
string data = "hehe";
char* str = const_cast<char*>(data.c_str());
cout << str << endl;
}
int main()
{
test_static_cast();
test_dynamic_cast();
test_reinterpret_cast();
test_const_cast();
return 0;
}