首页 > 代码库 > RTTI之typeid运算符

RTTI之typeid运算符

 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 Grand18 {19     public:20         Superb(int h=0):Grand(h){}21         void Speak() const {cout << "I am a superb class!\n";}22         virtual void Say() const23         {24             cout << "I hold the superb value of " << Value() << "!\n";25         }26 };27 28 class Magnificent:public Superb29 {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 Grand18 {19     public:20         Superb(int h=0):Grand(h){}21         void Speak() const {cout << "I am a superb class!\n";}22         virtual void Say() const23         {24             cout << "I hold the superb value of " << Value() << "!\n";25         }26 };27 28 class Magnificent:public Superb29 {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 Grand18 {19     public:20         Superb(int h=0):Grand(h){}21         void Speak() const {cout << "I am a superb class!\n";}22         virtual void Say() const23         {24             cout << "I hold the superb value of " << Value() << "!\n";25         }26 };27 28 class Magnificent:public Superb29 {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 }

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

类型;

结果为对象的表达式。

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

typeid(Magnificent)==typeid(*pg)

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

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