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

一、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中的固定位置(一般为第一个位置)(取决于具体编译器实现,标准并没有规定)。

#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 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++编程规范

转载自http://blog.csdn.net/jnu_simba/article/details/9320275

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值