the use of typeid

C++语言: Codee#25754
01 // type_info example
02 #include <iostream>
03 #include <typeinfo>
04 using namespace std;
05
06 struct Base {};
07 struct Derived : Base {};
08 struct Poly_Base
09 {
10     virtual void Member() {}
11 };
12 struct Poly_Derived: Poly_Base {};
13
14 int main()
15 {
16     // built-in types:
17     int i;
18     int * pi;
19     cout << "int is: " << typeid( int ). name() << endl;
20     cout << "  i is: " << typeid( i ). name() << endl;
21     cout << " pi is: " << typeid( pi ). name() << endl;
22     cout << "*pi is: " << typeid( * pi ). name() << endl << endl;
23
24     // non-polymorphic types:
25     Derived derived;
26     Base * pbase = & derived;
27     cout << "derived is: " << typeid( derived ). name() << endl;
28     cout << " *pbase is: " << typeid( * pbase ). name() << endl;
29     cout << boolalpha << "same type? ";
30     cout << ( typeid( derived) == typeid( * pbase) ) << endl << endl;
31
32     // polymorphic types:
33     Poly_Derived polyderived;
34     Poly_Base * ppolybase = & polyderived;
35     cout << "polyderived is: " << typeid( polyderived ). name() << endl;
36     cout << " *ppolybase is: " << typeid( * ppolybase ). name() << endl;
37     cout << boolalpha << "same type? ";
38     cout << ( typeid( polyderived) == typeid( * ppolybase) ) << endl << endl;
39 }
40 /*
41 int is: i
42   i is: i
43 pi is: Pi
44 *pi is: i
45
46 derived is: 7Derived
47 *pbase is: 4Base
48 same type? false
49
50 polyderived is: 12Poly_Derived
51 *ppolybase is: 12Poly_Derived
52 same type? true
53
54
55 Process returned 0 (0x0)   execution time : 0.041 s
56 Press any key to continue.
57
58
59 */

转载于:https://www.cnblogs.com/invisible/archive/2012/03/10/2388674.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值