一、RTTI

Run-time type information (RTTI) is a mechanism that allows the type of an object to be determined during program execution.

There are three main C++ language elements to run-time type information:

  • The dynamic_cast operator.

    Used for conversion of polymorphic types.

  • The typeid operator.

    Used for identifying the exact type of an object.

  • The type_info class.

    Used to hold the type information returned by the typeid operator.

    class type_info {
    public:

    virtual ~type_info();
        bool operator==(const type_info& rhs) const;
        bool operator!=(const type_info& rhs) const;
        int before(const type_info& rhs) const;
        const char* name() const;
        const char* raw_name() const;

    private:

    void *_m_data;
        char _m_d_name[1];
        type_info(const type_info& rhs);
        type_info& operator=(const type_info& rhs);
        static const char _Name_base(const type_info *,__type_info_node* __ptype_info_node);

    };

    The result of typeid is a const type_info&. The value is a reference to a type_info object that represents either the type-id or the type of the expression,
    depending on which form of typeid is used.

    为了支持RTTI,为每一个多态类创建一个type_info 对象(静态数据区),并把其地址保存到vtable中的固定位置(一般为第一个位置)(取决于具体编译器实现,标准并没有规定)。

     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
     
    #include <iostream>
    using namespace std;

    class Shape
    {
    public:
        virtual void Draw() = 0;
        virtual ~Shape() {}
    };

    class Circle : public Shape
    {
    public:
        void Draw()
        {
            cout << "Circle Draw ..." << endl;
        }
    };

    class Square : public Shape
    {
    public:
        void Draw()
        {
            cout << "Square Draw ..." << endl;
        }
    };

    int main(void)
    {
        Shape *p;
        Circle c;

    p = &c;
        p->Draw();

    //使用dynamic_cast 的条件
        //1、开启运行时类型信息;2、应用在具有多态关系的继承体系上;
        if (dynamic_cast<Circle *>(p))
        {
            cout << "p is point to a Circle object" << endl;
            Circle *cp = dynamic_cast<Circle *>(p);     // 安全向下转型
            cp->Draw(); //效率没有 p->Draw(); 高
        }
        else if (dynamic_cast<Square *>(p))
        {
            cout << "p is point to a Square object" << endl;
        }
        else
        {
            cout << "p is point to a Other object" << endl;
        }

    cout << typeid(*p).name() << endl;
        cout << typeid(Circle).name() << endl;
        if (typeid(Circle).name() == typeid(*p).name())
        {
            cout << "p is point to a Circle object" << endl;
            ((Circle *)p)->Draw();
        }
        else if (typeid(Square).name() == typeid(*p).name())
        {
            cout << "p is point to a Circle object" << endl;
            ((Square *)p)->Draw();
        }
        else
        {
            cout << "p is point to a Other object" << endl;
        }

    return 0;
    }

    如上所述,dynamic_cast 和 typeid 操作符
    都可以实现运行时类型识别。其中使用dynamic_cast
    时需要开启运行时类型信息,在项目-》属性-》C/C++-》语言-》启用运行时类型信息。在使用typeid时需要注意的是返回的是type_info
    对象的引用,且type_info 类的拷贝构造函数和赋值运算符都声明为私有,故不能这样写: type_info tf =
    typeid(Circle);

    二、类与类之间的关系

    Unified Modeling Language (UML)又称统一建模语言或标准建模语言,是始于1997年一个OMG标准,它是一个支持模型化和软件系统开发的图形化语言。

    1、继承(泛化)Generalization

    Manager 继承自Employee.

    2、关联 Association,单向关联 DirectedAssociation

    Order 作为Customer 的成员,如vector<Order>  orders ;

    3、聚合 Aggregation

    class B

    class A

    {

    public:

    B* b_;

    };

    当A释放时,不负责B的释放,也许B是被共享的。

    4、组合 Composition

    当Company 释放时要负责Department 的释放,Department 不是共享的。

    5、依赖 Dependency

    类A依赖于B:
    从语义上来上是A use B,偶然的,临时的
    B作为A的成员函数参数
    B作为A的成员函数的局部变量
    A的成员函数调用B的静态方法

    比较5种关系:

    继承体现的是类与类之间的纵向关系,其他4种体现的是类与类之间的横向关系。
    关联强弱

    依赖<关联<聚合<组合

    继承(A is B)
    关联、聚合、组合(A has B)
    依赖(A use B)

参考:

C++ primer 第四版
Effective C++ 3rd
C++编程规范

RTTI、dynamic_cast、typeid、类与类之间的关系uml的更多相关文章

  1. PHP类和对象之间的关系

    类是对象的描述: 类和对象之间的关系类似于建房子的图纸和房子: 创建类--new对象--用对象: 一.对象的两个特性: --对象的行为(功能):可以对对象施加操作,例如,电视机的开.关.转换频道: - ...

  2. JAVA面向对象-----接口与类、接口之间的关系

    接口与类.接口之间的关系 大家之前都知道类与类之间的关系继承,那么接口与类之间又是怎样子的关系呢? 接口与类之间是实现关系.非抽象类实现接口时,必须把接口里面的所有方法实现.类实现接口用关键字impl ...

  3. Python面向对象02/类的空间问题、类与对象之间的关系、类与类之间的关系

    Python面向对象02/类的空间问题.类与对象之间的关系.类与类之间的关系 目录 Python面向对象02/类的空间问题.类与对象之间的关系.类与类之间的关系 1. 类的空间问题 2. 类与对象之间 ...

  4. python 面向对象专题(二):类的空间问题、类与对象之间的关系、类与类之间的关系

    https://www.cnblogs.com/liubing8/p/11308127.html 目录 Python面向对象02/类的空间问题.类与对象之间的关系.类与类之间的关系 1. 类的空间问题 ...

  5. 从零开始学C++之RTTI、dynamic_cast、typeid、类与类之间的关系uml

    一.RTTI Run-time type information (RTTI) is a mechanism that allows the type of an object to be deter ...

  6. RTTI: dynamic_cast typeid

    dynamic_cast:将基类类型的指针向派生类指针安全转换.多用于下行转换.上行转换时,和static_cast是一样的.C++类型转换看这里.而const_cast用来修改类型的const或vo ...

  7. UML图类,接口之间的关系

    UML图类之间的关系(来自大话设计模式中的一张图,代表了类,接口之间的各种关系)

  8. 类与类之间的关系UML模型图

    关联.依赖.聚合.组合.泛化.实现 类之间可能存在以下几种关系:关联(association).依赖(dependency).聚合(Aggregation,也有的称聚集).组合(Composition ...

  9. 关于package,import,和“找不到可以加载的主类”报错之间的关系

    正在回顾java基础 目录结构如下: 一 以下代码,进入Example所在的文件夹, javac和java都不会报错 public class Example{ public static void ...

随机推荐

  1. Cwrsync_rsync windows_windows下的rsync

    1.官网已不允许免费下载cwrsync的server了,我就先给出下载地址: http://files.cnblogs.com/files/assassin1994/cwRsync_4.0.5_ser ...

  2. Retrieving ST-Link/V2 Firmware from Update Utility

    http://www.taylorkillian.com/2013/01/retrieving-st-linkv2-firmware-from.html http://forum.easyelectr ...

  3. 15KW电动机380V及220V时的电流分别为多少

    15KW电动机380V及220V时的电流分别为多少 当用电电压为380V时:P=UICOSφ/1.72,此时电流为: I=15KW/380V/0.83(COSφ,功率因数)/1.72x1000=27. ...

  4. MEF 导入(Import)和导出(Export)

    前言: MEF不同于其他IOC容器(如:Castle)很重要的原因在于它使用了特性化编程模型(涉及到两个概念:“特性”和“编程模型”). 特性(Attribute):举例来说就是我们在开发过程中在类上 ...

  5. java jdk查看源代码

    事实上假设你安装了JDK的话,你就已经拥有了java api的源代码. 安装JDK文件夹下的src.zip文件就是java api的源代码. 比方:C:\Program Files\Java\jdk1 ...

  6. 仿LOL项目开发第六天

    仿LOL项目开发第六天 by草帽 OK,因为更新模块已经处理好了,接着开始登陆的编写.那么我们就需要状态机的管理. 所谓状态机就是在哪个状态执行那个状态的代码逻辑: 那么我们开始编写GameState ...

  7. mvn sonar:sonar在jenkins步骤的执行位置影响执行结果

    1.如图所示,sonar执行可以在build中执行,也可以在步骤Post Steps中执行(mvn sonar:sonar) 2.但是在步骤Post Steps中执行的话,有一个问题,就是假如项目有多 ...

  8. DICOM:DICOM三大开源库对比分析之“数据加载”

    背景: 上一篇博文DICOM:DICOM万能编辑工具之Sante DICOM Editor介绍了DICOM万能编辑工具,在日常使用过程中发现,“只要Sante DICOM Editor打不开的数据,基 ...

  9. Linux下读取RFID卡号(C串口编程)

    由于项目需要用到RFID.GPRS.摄像头等模块所以便看了一下,整理了一下学习思路,本篇先是整理一下串口读取RFID卡号的程序思路,后面还会更其他的 RFID模块: 本次采用的是125K的RFID读卡 ...

  10. android:Layout_weight的深刻理解

    最近写Demo,突然发现了Layout_weight这个属性,发现网上有很多关于这个属性的有意思的讨论,可是找了好多资料都没有找到一个能够说的清楚的,于是自己结合网上资料研究了一下,终于迎刃而解,写出 ...