C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
 
/*
    名称:C++ 运行时类型识别
    作者:Michael Joessy
    日期:2017-06-06
    知识:Run-Time Type Information
    通过运行时类型信息程序能够使用基类的指针或引用来检查这些指针或引用所指的对象的实际派生类型。
    typeid  dynamic_cast
    注意:
    dynamic_cast:
        只能用于指针和引用的转换;
        要转换的类型中必须包含虚函数;
        转换成功返回子类的地址,失败返回NULL.
    typeid:
        typeid返回一个type_info(#include <typeinfo>)对象的引用;
        如果想通过基类的指针获得派生类的数据类型,基类必须带有虚函数;
        只能获取对象的实际类型.
*/

/*
    class type_info {
    public:
    virtual ~type_info();
    _CRTIMP_PURE bool __CLR_OR_THIS_CALL operator==(const type_info& rhs) const;
    _CRTIMP_PURE bool __CLR_OR_THIS_CALL operator!=(const type_info& rhs) const;
    _CRTIMP_PURE int __CLR_OR_THIS_CALL before(const type_info& rhs) const;
    _CRTIMP_PURE const char* __CLR_OR_THIS_CALL name(__type_info_node* __ptype_info_node = &__type_info_root_node) const;
    _CRTIMP_PURE const char* __CLR_OR_THIS_CALL raw_name() const;
    private:
    void *_m_data;
    char _m_d_name[1];
    __CLR_OR_THIS_CALL type_info(const type_info& rhs);
    type_info& __CLR_OR_THIS_CALL operator=(const type_info& rhs);
    _CRTIMP_PURE static const char *__CLRCALL_OR_CDECL _Name_base(const type_info *,__type_info_node* __ptype_info_node);
    _CRTIMP_PURE static void __CLRCALL_OR_CDECL _Type_info_dtor(type_info *);
    };
*/

#include <iostream>
#include <string>
#include <typeinfo>
using namespace std;

class Flyable
{
public:
    ;   //起飞
;      //着陆
         
protected:
private:
};

class Bird : public Flyable
{
public:
    void forageing()             //觅食
    {
        cout << "Bird->forageing()" << endl;
    }
    virtual void takeoff()       //起飞
    {
        cout << "Bird->takeoff()" << endl;
    }
    virtual void land()         //着陆 
    {
        cout << "Bird->land()" << endl;
    }
protected:
private:
};

class Plane : public Flyable
{
public:
    void carry()                 //运输
    {
        cout << "Plane->carry()" << endl;
    }
    virtual void takeoff()       //起飞
    {
        cout << "Plane->takeoff()" << endl;
    }
    virtual void land()          //着陆
    {
        cout << "Plane->land()" << endl;
    }
protected:
private:
};

void doSomething(Flyable* pObj)
{
    cout << typeid(*pObj).name() << endl;
    pObj->takeoff();
    if (typeid(*pObj) == typeid(Bird))
    {
        Bird* pBird = dynamic_cast<Bird*>(pObj);
        if (pBird)
        {
            pBird->forageing();
        }
    }
    if (typeid(*pObj) == typeid(Plane))
    {
        Plane* pPlane = dynamic_cast<Plane*>(pObj);
        if (pPlane)
        {
            pPlane->carry();
        }
    }
    pObj->land();
}

int main(void)
{
    Flyable* pObj1 = new Bird;
    Flyable* pObj2 = new Plane;
    doSomething(pObj1);
    doSomething(pObj2);

/************************************************************************/
    /* typeid usage                                           
    /************************************************************************/
    ;
    cout << typeid(nNumber).name() << endl;

Flyable* p = new Bird;
    cout << typeid(p).name() << endl;
    cout << typeid(*p).name() << endl;

cin.get();
    ;
}

C++之运行时类型识别RTTI的更多相关文章

  1. C++学习之显式类型转换与运行时类型识别RTTI

    static_cast const_cast reinterpret_cast 运行时类型识别(RTTI) dynamic_cast 哪种情况下dynamic_cast和static_cast使用的情 ...

  2. c++运行时类型识别(rtti)

    一个简单运行时类型识别 namespace rtti_ex { /* * 类型信息基类 */ class i_type_info { public: // 判断是否是指定类型 bool is(cons ...

  3. 运行时类型识别RTTI

    1.RTTI的工作原理 例1. 用Class加载对象示例. package RTTI; public class Candy { static{ System.out.println("Lo ...

  4. C++——运行时类型识别RTTI

    1.实现方式 typeid运算符,返回表达式的类型 dynamic_cast运算符,基类的指针或引用安全地转换成派生类的指针或引用 2.适用于:使用基类的指针或引用执行派生类的操作,且该操作不是虚函数 ...

  5. Java基础之RTTI 运行时类型识别

    运行时类型识别(RTTI, Run-Time Type Identification)是Java中非常有用的机制,在Java运行时,RTTI维护类的相关信息. 多态(polymorphism)是基于R ...

  6. RTTI 运行时类型识别 及异常处理

    RTTI   运行时类型识别 typeid  ------  dynamic_cast dynamic_cast 注意事项: 1.只能应用于指针和引用之间的转化 2.要转换的类型中必须包含虚函数 3. ...

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

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

  8. MFC六大核心机制之二:运行时类型识别(RTTI)

    上一节讲的是MFC六大核心机制之一:MFC程序的初始化,本节继续讲解MFC六大核心机制之二:运行时类型识别(RTTI). typeid运算子 运行时类型识别(RTTI)即是程序执行过程中知道某个对象属 ...

  9. 框架原理第二讲,RTTI,运行时类型识别.(以MFC框架讲解)

    框架原理第二讲,RTTI,运行时类型识别.(以MFC框架讲解) 一丶什么是RTTI,以及RTTI怎么设计 通过第一讲,我们知道了怎么样升成一个窗口了,以及简单的消息循环. 第二讲则是主要讲解RTTI ...

随机推荐

  1. 【BIRT】报表显示不全

    使用BIRT开发了一张报表,预期效果如下 但是开发完成后预览效果如下: 最后的合计竟然没有了,那么怎么处理呢 鼠标点击Layout窗口空白部分,找到布局,切换为自动布局,如下图所示:

  2. 基于Python Tornado的在线问答系统

    概述 本项目使用最新的Tornado开发.实现了在线提问,回答,评论等功能.使用到Tornado的generator,长轮询等等技术, 支持MySQL的异步连接. 详细 代码下载:http://www ...

  3. 【jquery操作cookie】JQuery中$.cookie()方法的使用(同名cookie会覆盖)

    jquery.cookie.js插件: <script type="text/javascript" src="js/jquery-1.6.2.min.js&quo ...

  4. Python实现百度搜索并保存到本地示例,Python实现百度搜索

    实现百度搜索并保存到本地 User_Agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko ...

  5. Web Socket rfc6455 握手 (C++)

    std::string data((const char*)buf->data(),bytes_transferred); recycle_buffer(buf); std::string ke ...

  6. 分布式ID生成方案

    系统唯一ID是设计一个系统的时候常常会遇到的问题,也常常为这个问题而纠结. 生成ID的方法有很多,适应不同的场景.需求以及性能要求.所以有些比较复杂的系统会有多个ID生成的策略. 0. 分布式ID要求 ...

  7. Atitit.软件仪表盘(0)--软件的子系统体系说明

    Atitit.软件仪表盘(0)--软件的子系统体系说明 1. 温度检测报警子系统 2. Os子系统 3. Vm子系统 4. Platform,业务系统子系统 5. Db数据库子系统 6. 通讯子系统 ...

  8. 线程相关函数(6)-pthread_cond_wait(),pthread_cond_signal(), 条件变量

    pthread_cond_tpthread_cond_initpthread_cond_destroypthread_cond_waitpthread_cond_timedwaitpthread_co ...

  9. 华农校赛--G,用set比较大小,缩短时间复杂度

    Array C Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 581  Solved: 101[Submit][Status][Web Board] D ...

  10. python学习笔记(1)--遍历txt文件,正则匹配替换文字

    遍历一个文件夹,把里面所有txt文件里的[]里的朗读时间删除,也就是替换为空. import os import re import shutil #os文件操作,re正则,shutil复制粘贴 pa ...