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. PTA 求链表的倒数第m个元素

    6-7 求链表的倒数第m个元素 (20 分)   请设计时间和空间上都尽可能高效的算法,在不改变链表的前提下,求链式存储的线性表的倒数第m(>)个元素. 函数接口定义: ElementType ...

  2. vue中的.sync修饰符用法

    在项目中接触到父组件传值给子组件的时候,想在子组件改变父组件传的值.(比如用于弹窗关闭) 但是正常来说,vue2是不允许子组件直接改父组件传进去的值的. 所以我们需要在子组件内定义自定义事件,通知父组 ...

  3. 计算机体系结构——CH1基本概念

    CH1基本概念 右键点击查看图像,查看清晰图像 CH1基本概念 目的与内容 了解计算机系统的完整概念 学习计算机系统的分析方法与设计方法 编写程序所必需了解的计算机属性 计算机系统结构简介 为什么要研 ...

  4. Windows下C++/Fortran调用.exe可执行文件

    目录 软件环境 Windows下CMake编译配置 设置项目的generator Command Line CMake GUI PreLoad.cmake 设置make 示例程序 CMake 设置Fo ...

  5. Dynamics CRM存放选项集记录的表

    我们在做一些自定义查询的时候会去查询选项集字段的值,但是实体的选项集字段是一个整型字段,直接查询并不能找到对应的选项集的显示内容.所以我们需要找到存放选项集键值对的表来做关联查询找到我们想要的值. D ...

  6. Sqlmap的基础用法(禁止用于非法用途,测试请自己搭建靶机)

    禁止用于非法用途,测试与学习请自己搭建靶机 sqlmap -r http.txt  #http.txt是我们抓取的http的请求包 sqlmap -r http.txt -p username  #指 ...

  7. js--原型和原型链相关问题

    前言 阅读本文前先来思考一个问题,我们在 js 中创建一个变量,我们并没有给这个变量添加一些方法,比如 toString() 方法,为什么我们可以直接使用这个方法呢?如以下代码,带着这样的问题,我们来 ...

  8. Maven相关知识总结

    目录 认识Maven Maven下载安装 Maven能用来做什么 Maven核心概念 开发目录 坐标和仓库 POM文件 POM文件内容 Maven依赖管理 构建生命周期 构建多模块系统 聚合 继承 聚 ...

  9. day8.函数基础

    一.函数介绍 1.什么是函数     函数就是盛放代码的容器,把实现某一功能的一组代码丢到一个函数中     就做成了一个小工具       具备某一功能的工具->函数     事先准备工具的过 ...

  10. Leecode第二题:两数相加

    Leecode2 先看题目 : 给你两个 非空 的链表,表示两个非负的整数.它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字. 请你将两个数相加,并以相同形式返回一个表示和的 ...