码迷,mamicode.com
首页 > 其他好文 > 详细

RTTI之typeid运算符

时间:2014-07-20 21:38:39      阅读:353      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   使用   os   

 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运算符

标签:style   blog   http   color   使用   os   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!