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. ASP.NET配置文件machine.config与性能[转]

    转 http://www.cnblogs.com/chenlulouis/archive/2010/05/26/1744261.html http://www.cnblogs.com/zhangron ...

  2. Django的ORM中如何判断查询结果是否为空,判断django中的orm为空

    result= Booking.objects.filter() #方法一 .exists() if result.exists(): print "QuerySet has Data&qu ...

  3. mysql 插入 详解

    表创建好后,就可以往里插入记录了,插入记录的基本语法如下: INSERT INTO tablename (field1,field2,……fieldn) VALUES(value1,value2,…… ...

  4. OpenCV2马拉松第24圈——轮廓提取

    计算机视觉讨论群162501053 转载请注明:http://blog.csdn.net/abcd1992719g/article/details/27979267 收入囊中 在图片中找到轮廓而且描绘 ...

  5. 禁止用户登陆的 /bin/false和/sbin/nologin的区别

    1 区别 /bin/false是最严格的禁止login选项,一切服务都不能用. /sbin/nologin只是不允许login系统  小技巧: 查看 /etc/passwd文件,能看到各用户使用的sh ...

  6. 进程控制函数(2)-setpgid() 修改当前进程的进程组ID

    定义:int setpgid(pid_t pid,pid_t pgid); 表头文件:#include<unistd.h> 说明:setpgid()将参数pid 指定进程所属的组识别码设为 ...

  7. action(四)

    void ActionDelayTime::onEnter() { ActionsDemo::onEnter(); alignSpritesLeft(); CCActionInterval* move ...

  8. ENGINE_API CXNoTouch

    /************************************************************************/ //屏蔽消息面板 //优先级默认为 TP_BOTT ...

  9. Jackson 时间格式化,时间注解 @JsonFormat 与 @DatetimeFormat 用法、时差问题说明

    @JsonFormat 使用 我们可以有两种用法(我知道的),在对象属性上,或者在属性的 getter 方法上,如下代码所示: 增加到属性上: ... ... /**更新时间 用户可以点击更新,保存最 ...

  10. 【C语言】C语言程序所占内存分类

    参考"http://blog.sina.com.cn/s/blog_63d4849c01014qg3.html" C语言内存分为5部分:堆.栈.全局(静态)区.常量区(只读)和代码 ...