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 }

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运算符的更多相关文章

  1. RTTI: dynamic_cast typeid

    dynamic_cast:将基类类型的指针向派生类指针安全转换.多用于下行转换.上行转换时,和static_cast是一样的.C++类型转换看这里.而const_cast用来修改类型的const或vo ...

  2. C++中模板单例的跨SO(DLL)问题:RTTI,typeid,static,单例

    (转载请注明原创于潘多拉盒子) C++的模板可以帮助我们编写适合不同类型的模板类,给代码的复用性提供了极大的方便.近来写了一个涉及单例的C++模板类,简化下来可以归结为以下的代码: template ...

  3. 如何在C++中获得完整的类型名称(RTTI的typeid在不同平台下有不同的输出值表达,自建类改进了RTTI丢失的信息)

    Wrote by mutouyun. (http://darkc.at/cxx-get-the-name-of-the-given-type/)   地球人都知道C++里有一个typeid操作符可以用 ...

  4. RTTI之dynamic_cast运算符

    #include <iostream> #include <cstdlib> #include <ctime> using std::cout; class Gra ...

  5. RTTI

    RTTI(Run-Time Type Identification,通过运行时类型识别)程序能够使用基类的指针或引用来检查这些指针或引用所指的对象的实际派生类型.   编辑本段RTTI介绍 RTTI提 ...

  6. RTTI (Run-Time Type Identification,通过运行时类型识别) 转

    参考一: RTTI(Run-Time Type Identification,通过运行时类型识别)程序能够使用基类的指针或引用来检查这些指针或引用所指的对象的实际派生类型.   RTTI提供了以下两个 ...

  7. RTTI(Runtime Type Information )

    RTTI 是“Runtime Type Information”的缩写,意思是:运行时类型信息.它提供了运行时确定对象类型的方法.本文将简略介绍 RTTI 的一些背景知识.描述 RTTI 的概念,并通 ...

  8. c++ RTTI(runtime type info)

    RTTI(Run-Time Type Information,通过运行时类型信息)程序能够使用基类的指针或引用来检查这些指针或引用所指的对象的实际派生类型. RTTI提供了以下两个非常有用的操作符: ...

  9. RTTI,C++类型转换操作符

    body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...

随机推荐

  1. C# yield return 原理探究

    天需要些一个小工具,需要使用到多线程读写程序集,接口方法返回值类型需要为"IEnumerable<string>"这里用到了"yield return&quo ...

  2. 【设计模式】- 生成器模式(Builder)

    生成器模式 建造者模式.Builder 生成器模式 也叫建造者模式,可以理解成可以分步骤创建一个复杂的对象.在该模式中允许你使用相同的创建代码生成不同类型和形式的对象. 生成器的结构模式 生成器(Bu ...

  3. OO_Unit1_Summary

    经历了十分充实(痛不欲生)的三周不一样的码代码的生活,让我对通宵oo有了新的认识.往届学长学姐诚不欺我 第一次作业 需求分析 第一次需求非常简单(相比较后两次作业而言),仅为简单多项式求导,而且仅包含 ...

  4. (九)Struts2模型驱动和属性驱动

    出于结构清晰的考虑,应该采用单独的Model实例来封装请求参数和处理结果,这就是所谓的模型驱动, 所谓模型驱动,就是使用单独的JavaBean来贯穿整个MVC流程. 所谓属性驱动,就是使用属性来作为贯 ...

  5. 第一个 vue-cli项目

    第一个 vue-cli项目 什么是vue-cli vue-cli官方提供的一个脚手架,用于快速生成一个vue的项目模板: 预先定义好的目录结构及基础代码,就好比咱们在创建Maven项目时可以选择创建一 ...

  6. hbuilderX打包苹果证书的申请方法

    现在uniapp越来越火,hbuilderX和apicloud这些工具使用html+js语言就可以开发强大的app,大大降低了app开发的技术门槛. hbuilderX或apicloud在打包ios应 ...

  7. Github仓库master分支到main分支迁移指南

    1 概述 2020年10月1日后,Github会将所有新建的仓库的默认分支从master修改为main,这就导致了一些旧仓库主分支是master,新仓库主分支是main的问题,这在有时候会带来一些麻烦 ...

  8. Boxes in a Line UVA - 12657

      You have n boxes in a line on the table numbered 1...n from left to right. Your task is to simulat ...

  9. Mycat Web监控工具

    简介 Mycat-web 是 Mycat 可视化运维的管理和监控平台,弥补了 Mycat 在监控上的空白.帮 Mycat 分担统计任务和配置管理任务.Mycat-web 引入了 ZooKeeper 作 ...

  10. Day07_36_Iterator迭代器

    Iterator Iterator Iterator iterator(); 获取集合所依赖的迭代对象 通过迭代器iterator()中的方法完成集合的迭代(遍历),这种方式是所有集合通用的遍历方法. ...