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系列教程-Spring 教程目录

    Spring 教程目录 可参考MyBatis的官方文档也比较清楚 https://mybatis.org/mybatis-3/zh/getting-started.html 代码 目录 https:/ ...

  2. Linux—用户新建目录和文件的默认权限设置:umask详解

    关注微信公众号:CodingTechWork,一起学习进步. 引言   我们有没有思考过一个问题,在登录Linux系统后,我们创建的目录或者文件的权限,为什么每次创建都是统一的?我们做以下实验:新建一 ...

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

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

  4. 庐山真面目之十二微服务架构基于Docker搭建Consul集群、Ocelot网关集群和IdentityServer版本实现

    庐山真面目之十二微服务架构基于Docker搭建Consul集群.Ocelot网关集群和IdentityServer版本实现 一.简介      在第七篇文章<庐山真面目之七微服务架构Consul ...

  5. HTML总结篇

    一.HTML基本结构标签 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...

  6. OOP第一次博客作业

    一.关于Java&&面向对象 本学期刚开始进行Java的学习,也是刚开始了解面向对象,目前也就学习了三四周的样子,期间进行了三次作业,我感觉到Java的语法和c语言中的有许多相似之处, ...

  7. 使用ffmpeg 操作音频文件前后部分静音移除.

    指令特别简单, 但是却琢磨了一下午. 总结看文档时要细心, 主要ffmpeg的版本要 8.2.1 以上 ffmpeg -i in.mp3 -af silenceremove=start_periods ...

  8. Redis初学

    1. redis     1. 概念     2. 下载安装     3. 命令操作         1. 数据结构     4. 持久化操作     5. 使用Java客户端操作redis Redi ...

  9. 【CTF】图片隐写术 · 盲水印

    前言 盲水印同样是CTF Misc中极小的一个知识点,刚刚做到一题涉及到这个考点的题目. 感觉还挺有意思的,就顺便去了解了下盲水印技术. 数字水印 数字水印(Digital Watermark)一种应 ...

  10. 从中国加入WTO来看Java设计模式:中介者模式

    目录 应用场景 中介者模式 定义 意图 主要解决问题 何时使用 优缺点 世界贸易组织WTO 应用场景 系统中对象之间存在比较复杂的引用关系,导致它们之间的依赖关系结构混乱而且难以复用该对象 想通过一个 ...