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. Java并发编程之锁机制

    锁分类 悲观锁与乐观锁 悲观锁认为对于同一个数据的并发操作,一定是会发生修改的,哪怕没有修改,也会认为修改.因此对于同一个数据的并发操作,悲观锁采取加锁的形式.悲观的认为,不加锁的并发操作一定会出问题 ...

  2. ElasticSearch实战系列十: ElasticSearch冷热分离架构

    前言 本文主要介绍ElasticSearch冷热分离架构以及实现. 冷热分离架构介绍 冷热分离是目前ES非常火的一个架构,它充分的利用的集群机器的优劣来实现资源的调度分配.ES集群的索引写入及查询速度 ...

  3. [单调栈]Imbalanced Array

    I m b a l a n c e d A r r a y Imbalanced Array ImbalancedArray 题目描述 You are given an array a a a con ...

  4. [Fundamental of Power Electronics]-PART I-4.开关实现-0 序

    4 开关实现 在前面的章节中我们已经看到,可以使用晶体管,二极管来作为Buck,Boost和其他一些DC-DC变换器的开关元件.也许有人会想为什么会这样,以及通常如何实现半导体的开关.这些都是值得被提 ...

  5. 201871030108-冯永萍 实验二 个人项目— D{0-1}背包问题项目报告

    项目 内容 课程班级博客链接 https://edu.cnblogs.com/campus/xbsf/2018CST 这个作业要求链接 https://www.cnblogs.com/nwnu-dai ...

  6. 五、python学习-面向对象

    1.面对对象程序开发基础(oop) 面对对象:高内聚 低耦合 面向过程: 优点:效率高,执行速度快 缺点:维护性,移植性差,表达不出一类的语义 面向对象: 优点:可读性,可移植性,可维护性高 缺点:执 ...

  7. 因为这几个TypeScript代码的坏习惯,同事被罚了500块

    作者:Daniel Bartholomae 翻译:疯狂的技术宅 原文链接:https://startup-cto.net/10-bad-typescript-habits-to-break-this- ...

  8. 创建支持依赖注入、Serilog 日志和 AppSettings 的 .NET 5 控制台应用

    翻译自 Mohamad Lawand 2021年3月24日的文章 <.NET 5 Console App with Dependency Injection, Serilog Logging, ...

  9. 网络编程Netty入门:责任链模式介绍

    目录 责任链模式 责任链模式的简单实现 Netty中的ChannelPipeline责任链 服务端接收客户端连接 pipeline初始化 入站事件和出站事件 Pipeline中的Handler Pip ...

  10. Socket 多任务(多进程/线程、I/O多路复用、事件驱动开发框架)

    0. 概述 1. 循环版实现多连接 2. threading.Thread 多线程 3. SockerServer 实现多任务 3.1 ForkingMixIn - 多进程(限 linux) 3.2 ...