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. Android 图片混排富文本编辑器控件

    概述 一个Android 图片混排富文本编辑器控件(仿兴趣部落) 详细 代码下载:http://www.demodashi.com/demo/12032.html 一.一个Android 图片混排富文 ...

  2. EL和自定义函数库

    问题:在数据显示的时候经常需要调用一些方法对需要显示的数据进行基本的处理,如: 数据过滤.求子串等操作.那么就需要使用EL表达式进行快速的函数调用. 引入HTMLFilter.java类 描述为函数 ...

  3. 【LeetCode】102. Binary Tree Level Order Traversal (2 solutions)

    Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...

  4. C++:CursorType光标类型 和 LockType锁定类型

    简要: CursorType光标类型: 1.   AdOpenForwardOnly   (默认值)一次只能向前移动一行. 2.   AdOpenKeyset   打开键集类型游标. 3.   AdO ...

  5. 关于GridView Master-Detail 不支持明细属性为IEnumerable、IList问题

    默认状态下gridview不支持接口集合,即不支持属性类型为IEnumerable<T>或者扩展的IList<T>,只能乖乖的转成List等实体集合,这种取舍就是鱼和熊掌了,如 ...

  6. redis基础之基本键值操作和使用(三)

    前言 redis安装完毕后开始使用redis,先熟悉命令行操作. redis数据的类型 键:redis的所有的键都是string类型: 值:五种类型 string:字符串类型:一个string最大可以 ...

  7. Linux 进程资源用量监控和按用户设置进程限制

    每个 Linux 系统管理员都应该知道如何验证硬件.资源和主要进程的完整性和可用性.另外,基于每个用户设置资源限制也是其中一项必备技能. 在这篇文章中,我们会介绍一些能够确保系统硬件和软件正常工作的方 ...

  8. angular路由介绍

    AngularJS路由功能是一个纯前端的解决方案,与我们熟悉的后台路由不太一样.后台路由,通过不同的URL会路由到不同的控制器上(controller),再渲染(render)到页面(HTML).An ...

  9. apue编程之参考du代码利用递归写的一个简单的du命令的源代码

    #include <stdio.h> #include <stdlib.h> #include <glob.h> #include <string.h> ...

  10. C++函数默认参数(转)

    在代码中使用到了函数的默认参数,在函数的定义和实现中都填写的默认参数,结果出现了错误: 代码: #ifndef FIRSTPAGE_H #define FIRSTPAGE_H #include < ...