• 66 - C++中的类型识别


    ---- 整理自狄泰软件唐佐林老师课程

    1. 类型识别

    在面向对象中可能出现下面的情况:

    • 基类指针指向子类对象
    • 基类引用成为子类对象的别名

    这个时候就会出现问题:(由于 赋值兼容性原则 )没法通过一个父类指针判断指向的是父类对象还是子类对象

    在这里插入图片描述

    • 静态类型变量(对象)自身的类型
    • 动态类型指针(引用)所指向对象的实际类型
      在这里插入图片描述

    基类指针是否可以强制类型转换为子类指针取决于动态类型

    1.1 问题

    C++中如何得到 动态类型

    1.2 解决方案:利用 多态

    • 在基类中定义虚函数返回具体的类型信息
    • 所有的派生类都必须实现类型相关的虚函数
    • 每个类中的类型虚函数都需要不同的实现

    1.3 编程实验:动态类型识别

    #include 
    #include 
    
    using namespace std;
    
    class Base
    {
    public:
        virtual string type()
        {
            return "Base";
        }
    };
    
    class Derived : public Base
    {
    public:
        string type()
        {
            return "Derived";
        }
        
        void printf()
        {
            cout << "I'm a Derived." << endl;
        }
    };
    
    class Child : public Base
    {
    public:
        string type()
        {
            return "Child";
        }
    };
    
    void test(Base* b)
    {
        /* 危险的转换方式 */
        // Derived* d = static_cast(b);
        
        if( b->type() == "Derived" )
        {
            Derived* d = static_cast<Derived*>(b);
            
            d->printf();
        }
        
        // cout << dynamic_cast(b) << endl;
    }
    
    int main(int argc, char *argv[])
    {
        Base b;
        Derived d;
        Child c;
        
        test(&b);
        test(&d);
        test(&c);
        
        return 0;
    }
    
    • 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

    在这里插入图片描述

    向上/向下转换可参看 55 - 经典问题解析四(动态内存分配&虚函数&继承中的强制类型转换)

    1.4 多态解决方案的缺陷

    • 必须从基类开始提供类型虚函数
    • 所有的派生类都必须重写类型虚函数
    • 每个派生类的类型名必须唯一

    1.5 类型识别关键字

    • C++提供了 typeid关键字 用于获取类型信息
      • typeid关键字 返回对应参数的 类型信息
      • typeid返回一个type_info类对象
      • 当typeid的参数为NULL时将抛出异常

    1.5.1 typeid关键字的使用

    int i = 0;			
    const type_info& tiv = typeid(i);			
    const type_info& tii = typeid(int);	
    cout << (tiv == tii) << endl;	
    
    • 1
    • 2
    • 3
    • 4

    1.5.2 typeid的注意事项

    • 当参数为 类型 时:返回静态类型信息
    • 当参数为 变量 时:
      • 不存在虚函数表 – 返回静态类型信息
      • 存在虚函数表 – 返回动态类型信息

    1.5.3 编程实验:typeid类型识别

    #include 
    #include 
    #include 
    
    using namespace std;
    
    class Base
    {
    public:
        virtual ~Base()
        {
        }
    };
    
    class Derived : public Base
    {
    public:
        void printf()
        {
            cout << "I'm a Derived." << endl;
        }
    };
    
    void test(Base* b)
    {
        const type_info& tb = typeid(*b);
        
        cout << tb.name() << endl;
    }
    
    int main(int argc, char *argv[])
    {
        int i = 0;
        
        const type_info& tiv = typeid(i);
        const type_info& tii = typeid(int);
        
        cout << (tiv == tii) << endl;
        
        Base b;
        Derived d;
        
        test(&b);
        test(&d);
        
        return 0;
    }
    
    • 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

    在这里插入图片描述
    在这里插入图片描述

    2. 小结

    • C++中有静态类型和动态类型的概念
    • 利用多态能够实现对象的动态类型识别
    • typeid是专用于 类型识别 的关键字
    • typeid能够返回对象的动态类型信息
  • 相关阅读:
    【Linux】基础IO,软硬链接,动静态库
    Swift数据类型String、Int、Float、Double转换
    细谈MySQL的一下经典问题(一)
    python打卡提醒机器人(企业微信)
    【EI检索】2022年电子、通信与控制工程国际会议SECCE 2022
    计算机网络工程师多选题系列——计算机网络
    金仓数据库KingbaseES ksql工具用户指南及参考--3. Ksql入门
    设计模式22——备忘录模式
    Hadoop集群配置相关架构介绍
    NSSCTF-Web题目18(反序列化)
  • 原文地址:https://blog.csdn.net/weixin_36098975/article/details/128114432