1 #include <iostream>
2 #include <typeinfo>
3 using namespace std;
4 class X{virtual void foo(){}};
5 class Y:public X{void foo(){}};
6 class Z:public X{void foo(){}};
7
8 void func(X &x){
9 if(typeid(x) == typeid(Y)){
//判断两个对象类型是否相同 10 cout << "对Y对象处理" << endl;
11 }else if(typeid(x) == typeid(Z)){
12 cout << "对Z对象处理" << endl;
13 }
14 }
15 int main(void){
16 int x;
17 cout << typeid(int).name() << endl;
//i 18 cout << typeid(x).name() << endl;
//i 19 cout << typeid(char).name() << endl;
//c
20 cout << typeid(unsigned int).name() << endl;
//j 21 cout << typeid(double[10]).name() << endl;
//A10_d
22 cout << typeid(int *[5]).name() << endl;
//A5_Pi
23 cout << typeid(int (*)[5]).name() << endl;
//PA5_i
24 cout << typeid(int (*[5])(int)).name() << endl;
//A5_PFiiE
25 class AA{};
26 cout << typeid(AA).name() << endl;
27 X *px = new Y;
28 cout << typeid(*px).name() << endl;
29
30 Y y;
31 func(y);
32 Z z;
33 func(z);
34 return 0;
35 }
tarena@ubuntu:~/cplus/day11$ ./a.out
i
i
c
j
A10_d
A5_Pi
PA5_i
A5_PFiiE
Z4mainE2AA
1Y
对Y对象处理
对Z对象处理
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。