首页 > 其他 > 详细

RTTI之typeid运算符

时间:2014-07-20 21:38:54      阅读:418      评论:0      收藏:0      [点我收藏+]
 1 #include <iostream>
 2 #include <cstdlib>
 3 #include <ctime>
 4 #include <typeinfo>
 5 
 6 using std::cout;
 7 class Grand
 8 {
 9     private:
10         int hold;
11     public:
12         Grand(int h=0):hold(h){}
13         virtual void Speak() const {cout << "I am a grand class!\n";}
14         virtual int Value() const {return hold;}
15 };
16 
17 class Superb:public Grand
18 {
19     public:
20         Superb(int h=0):Grand(h){}
21         void Speak() const {cout << "I am a superb class!\n";}
22         virtual void Say() const
23         {
24             cout << "I hold the superb value of " << Value() << "!\n";
25         }
26 };
27 
28 class Magnificent:public Superb
29 {
30     private:
31         char    ch;
32     public:
33         Magnificent(int h=0, char c=A) : Superb(h),ch(c){}
34         void Speak() const {cout << "I am a magnificent class!!!\n";}
35         void Say() const {cout << "I hold the character " << ch << " and th e integer " << Value() << "!\n";}
36 };
37 
38 Grand * Getone();
39 
40 int main()
41 {
42     std::srand(std::time(0));
43     Grand * pg;
44     Superb * ps;
45     for(int i=0;i<5;i++)
46     {
47         pg=Getone();
48         cout << "Now processing type " << typeid(*pg).name() << ".\n";
49         pg->Speak();
50         if(ps=dynamic_cast<Superb *>(pg))
51             ps->Say();
52         if(typeid(Magnificent)==typeid(*pg))
53             cout << "Yes, you‘re really magnificent.\n";
54     }
55     return 0;
56 }
57 
58 Grand * Getone()
59 {
60     Grand * p;
61     switch(std::rand()%3)
62     {
63         case 0: p=new Grand(std::rand()%100);
64                 break;
65         case 1:p=new Superb(std::rand()%100);
66                break;
67         case 2:p=new Magnificent(std::rand()%100,A+std::rand()%26);
68                break;
69     }
70     return p;
71 }
 1 #include <iostream>
 2 #include <cstdlib>
 3 #include <ctime>
 4 #include <typeinfo>
 5 
 6 using std::cout;
 7 class Grand
 8 {
 9     private:
10         int hold;
11     public:
12         Grand(int h=0):hold(h){}
13         virtual void Speak() const {cout << "I am a grand class!\n";}
14         virtual int Value() const {return hold;}
15 };
16 
17 class Superb:public Grand
18 {
19     public:
20         Superb(int h=0):Grand(h){}
21         void Speak() const {cout << "I am a superb class!\n";}
22         virtual void Say() const
23         {
24             cout << "I hold the superb value of " << Value() << "!\n";
25         }
26 };
27 
28 class Magnificent:public Superb
29 {
30     private:
31         char    ch;
32     public:
33         Magnificent(int h=0, char c=A) : Superb(h),ch(c){}
34         void Speak() const {cout << "I am a magnificent class!!!\n";}
35         void Say() const {cout << "I hold the character " << ch << " and th e integer " << Value() << "!\n";}
36 };
37 
38 Grand * Getone();
39 
40 int main()
41 {
42     std::srand(std::time(0));
43     Grand * pg;
44     Superb * ps;
45     for(int i=0;i<5;i++)
46     {
47         pg=Getone();
48         cout << "Now processing type " << typeid(*pg).name() << ".\n";
49         pg->Speak();
50         if(ps=dynamic_cast<Superb *>(pg))
51             ps->Say();
52         if(typeid(Magnificent)==typeid(*pg))
53             cout << "Yes, you‘re really magnificent.\n";
54     }
55     return 0;
56 }
57 
58 Grand * Getone()
59 {
60     Grand * p;
61     switch(std::rand()%3)
62     {
63         case 0: p=new Grand(std::rand()%100);
64                 break;
65         case 1:p=new Superb(std::rand()%100);
66                break;
67         case 2:p=new Magnificent(std::rand()%100,A+std::rand()%26);
68                break;
69     }
70     return p;
71 }
 1 #include <iostream>
 2 #include <cstdlib>
 3 #include <ctime>
 4 #include <typeinfo>
 5 
 6 using std::cout;
 7 class Grand
 8 {
 9     private:
10         int hold;
11     public:
12         Grand(int h=0):hold(h){}
13         virtual void Speak() const {cout << "I am a grand class!\n";}
14         virtual int Value() const {return hold;}
15 };
16 
17 class Superb:public Grand
18 {
19     public:
20         Superb(int h=0):Grand(h){}
21         void Speak() const {cout << "I am a superb class!\n";}
22         virtual void Say() const
23         {
24             cout << "I hold the superb value of " << Value() << "!\n";
25         }
26 };
27 
28 class Magnificent:public Superb
29 {
30     private:
31         char    ch;
32     public:
33         Magnificent(int h=0, char c=A) : Superb(h),ch(c){}
34         void Speak() const {cout << "I am a magnificent class!!!\n";}
35         void Say() const {cout << "I hold the character " << ch << " and th e integer " << Value() << "!\n";}
36 };
37 
38 Grand * Getone();
39 
40 int main()
41 {
42     std::srand(std::time(0));
43     Grand * pg;
44     Superb * ps;
45     for(int i=0;i<5;i++)
46     {
47         pg=Getone();
48         cout << "Now processing type " << typeid(*pg).name() << ".\n";
49         pg->Speak();
50         if(ps=dynamic_cast<Superb *>(pg))
51             ps->Say();
52         if(typeid(Magnificent)==typeid(*pg))
53             cout << "Yes, you‘re really magnificent.\n";
54     }
55     return 0;
56 }
57 
58 Grand * Getone()
59 {
60     Grand * p;
61     switch(std::rand()%3)
62     {
63         case 0: p=new Grand(std::rand()%100);
64                 break;
65         case 1:p=new Superb(std::rand()%100);
66                break;
67         case 2:p=new Magnificent(std::rand()%100,A+std::rand()%26);
68                break;
69     }
70     return p;
71 }

bubuko.com,布布扣

typeid运算符能够确定两个对象是否为同种类型。它与sizeof有些相像,可以接受两种参数:

类型;

结果为对象的表达式。

typeid运算符返回一个对type_info对象的引用,其中,type_info是在头文件typeinfo中定义的一个类。type_info类重在了==和!==运算符,以便可以使用这些运算符来对类型进行比较。例如,如果pg指向的是一个Magnificent对象,则下述表达式返回true,否则为false:

typeid(Magnificent)==typeid(*pg)

如果pg是一个空指针,程序将引发bad_typeid异常。

type_info类的实现随厂商而异,但包含一个name()成员,该函数返回一个随实现而异的字符串,通常为类的名称。本程序返回的是类的名称长度+类的名称。

RTTI之typeid运算符,布布扣,bubuko.com

RTTI之typeid运算符

原文:http://www.cnblogs.com/lakeone/p/3856393.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!