一、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 thetypeid 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 thetype-id or the type of theexpression, depending on which form oftypeid is used.

     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() = 
    ;

        
    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 
    ;

    }

    如上所述,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++编程规范

从零开始学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. 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. Java 中判断类和实例之间的关系

    判断类与实例的关系有以下三种方式 1.instanceof关键字,用来判断对象是否是类的实例 (对象 => 类 )   2.isAssignableFrom,用来判断类型间是否存在派生关系 (类 ...

随机推荐

  1. 安装后维修指南再次双系统xp、win7、ubuntu

    在本文中,主机为单个硬盘,三个步骤. 第一步: 使用liveCD要么u盘引导计算机进入ubuntu.这里最好是确保两个系统版本号之间的一致性. 菜单 System -> Administrati ...

  2. Spring IOC之 使用JSR 330标准注解

    从Spring 3.0开始,Spring提供了对 JSR 330标准注解的支持.这些注解可以喝Spring注解一样被扫描到.你只需要将相关的Jar包加入到你的classpath中即可. 注意:如果你使 ...

  3. 图文详解ReSharper 8.1功能变化

    ReSharper 8.1版本已经发布有段时间了,被广大开发者购买和试用,今天小编就ReSharper 8.1功能变化就行详细的解说. 支持TypeScript 突出了重构(重命名,引入变量).导航. ...

  4. weblogic生产、开发模式互转

    生产模式与开发模式转换: 1.生产模式-->开发模式     将%DOMAIN_HOME%\config\config.xml文件中<production-mode-enabled> ...

  5. 【SSRS】入门篇(六) -- 分组和总计

    原文:[SSRS]入门篇(六) -- 分组和总计 通过[SSRS]入门篇(五) -- 设置报表格式的设计,一份简单格式的报表已产生,如下图: 这节来说说分组和总计: 根据日期.订单对数据进行分组 添加 ...

  6. Android项目---常用动画

    在项目中经常会有闪屏的效果 在这里主要是通过定时器,将已经设定好的效果展现出来 /* * 2.5秒以后开始执行Runnable的run方法 */ new Handler().postDelayed(n ...

  7. NhibernateProfiler-写个自动破解工具(源码)

    04 2013 档案   [屌丝的逆袭系列]是个人都能破解之终结NhibernateProfiler-写个自动破解工具(源码) 摘要: 破解思路分析及手动破解 增加“附加到进程”功能--功能介绍增加“ ...

  8. 基于MEF的插件框架之总体设计

    基于MEF的插件框架之总体设计 1.MEF框架简介 MEF的全称是Managed Extensibility Framework(MEF),其是.net4.0的组成部分,在3.5上也可以使用.熟悉ja ...

  9. 对Extjs中store的多种操作

    Store.getCount()返回的是store中的所有数据记录,然后使用for循环遍历整个store,从而得到每条记录. 除了使用getCount()的方法外,还可以使用each()函数,如下面的 ...

  10. NUint使用详解及Visual Studio配置

    NUint使用详解及Visual Studio配置 阅读目录 什么是单元测试? 为什么使用单元测试? NUint使用详解: 示例 属性 断言 简单测试 VS配置: External Tools Vis ...