zoukankan      html  css  js  c++  java
  • C++解析(29):类型识别

    0.目录

    1.类型识别

    2.动态类型识别

    3.类型识别关键字

    4.小结

    1.类型识别

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

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

    • 静态类型——变量(对象)自身的类型
    • 动态类型——指针(引用)所指向对象的实际类型

    2.动态类型识别

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

    解决方案——利用多态

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

    示例——利用多态动态类型识别:

    #include <iostream>
    
    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<Derived*>(b);
        
        if( b->type() == "Derived" )
        {
            Derived* d = static_cast<Derived*>(b);
            
            d->printf();
        }
        
        // cout << dynamic_cast<Derived*>(b) << endl;
    }
    
    int main(int argc, char *argv[])
    {
        Base b;
        Derived d;
        Child c;
        
        test(&b);
        test(&d);
        test(&c);
        
        return 0;
    }
    

    运行结果为:

    [root@bogon Desktop]# g++ test.cpp
    [root@bogon Desktop]# ./a.out 
    I'm a Derived.
    

    多态解决方案的缺陷:

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

    3.类型识别关键字

    C++提供了typeid关键字用于获取类型信息:

    • typeid关键字返回对应参数的类型信息
    • typeid返回一个type_info类对象
    • typeid的参数为NULL时将抛出异常

    typeid关键字的使用:

    typeid的注意事项

    • 参数为类型时:返回静态类型信息
    • 参数为变量时:
      1. 不存在虚函数表——返回静态类型信息
      2. 存在虚函数表——返回动态类型信息

    示例——typeid动态类型识别:

    #include <iostream>
    #include <typeinfo>
    
    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;
    }
    

    运行结果为:

    [root@bogon Desktop]# g++ test.cpp
    [root@bogon Desktop]# ./a.out 
    1
    4Base
    7Derived
    

    typeid在不同的编译器下行为不一样(也就是说打印出来的类型名是不一样的)。

    4.小结

    • C++中有静态类型动态类型的概念
    • 利用多态能够实现对象的动态类型识别
    • typeid是专用于类型识别的关键字
    • typeid能够返回对象的动态类型信息
  • 相关阅读:
    【车】汽车X40保养
    【Teradata SQL】使用SQL将多个逗号分隔改为一个逗号分隔
    【Teradata】并行操作工具
    【Linux常见问题】SecureCRT 终端连接密钥交换失败错误
    【Linux基础】Linux更改系统IP
    【Teradata】块压缩(ferret工具)
    【Teradata】grouping和rollup窗口函数
    HttpClient之EntityUtils工具类
    MYSQL索引优化之单表示例
    MySQL执行计划示例
  • 原文地址:https://www.cnblogs.com/PyLearn/p/10095652.html
Copyright © 2011-2022 走看看