【C++】 66_C++ 中的类型识别

发布时间:2019-07-03 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了【C++】 66_C++ 中的类型识别脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

类型识别

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

    • 基类指针指向子类对象(赋值兼容性)
    • 基类引用成为子类对象的别名(赋值兼容性)

clipboard.png

  • 静态类型 - 变量(对象)自身的类型【编译期可确定】
  • 动态类型 - 指针(引用)所指向对象的实际类型【运行时才可确定】
void test(Base* b)
{
    /** 危险的类型转换方式:无法确定实际指向的类型 */
    Derived* d = static_cast<Derived*>(b);
}

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

问题:
C++ 中如何得到动态类型呢?

动态类型识别

  • 解决方案 - 利用多态

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

编程实验: 动态类型识别

#include <iostream>
#include <string>

using namespace std;

class Base
{
public:
    virtual string type()
    {
        return "Base";
    }
};

class Derived : public Base
{
public:
    string type()
    {
        return "Derived";
    }
    
    void print()
    {
        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->print();
    }
    
    // cout << dynamic_cast<Derived*>(b) << endl;
}

int main()
{
    Base b;
    Derived d;
    Child c;
    
    test(&amp;b);
    test(&d);
    test(&c);

    return 0;
}
输出:
I'm a Derived.

思考:dynamic_cast<Derived*>(b); 为什么不适用于这里呢?
dynamic_cast 可以判断是否转换成功,但无法识别指针实际指向的类型
  • 多态解决方案的缺陷

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

可维护性不够友好

类型识别关键字

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

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

  • typeid 关键字的使用
void code()
{
    int i = 0;
    
    const type_info& tiv = typeid(i);
    const type_info& tii = typeid(int);
    
    cout << (tiv == tii) << endl;
}
  • typeid 的注意事项

    • 当参数为类型时: 返回静态类型信息
    • 当参数为变量时:

      • 不存在虚函数表 - 返回静态类型信息
      • 存在虚函数表 - 返回动态类型信息

编程实验: typeid 类型识别

#include <iostream>
#include <string>
#include <typeinfo>

using namespace std;

class Base
{
public:
    virtual ~Base()
    {
    }
};

class Derived : public Base
{
public:
    void print()
    {
        cout << "I'm a Derived." << endl;
    }
};

void test(Base* b)
{
    const type_info& tb = typeid(*b);
    
    cout << tb.name() << endl;
}

int main()
{
    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;
}
输出【g++】
1
4Base
7Derived

输出【vc2010】
1
class Base
class Derived

输出【bcc】
1
Base
Derived

注意: typeid 虽然是 C++ 关键字,但在不同的编译器下有不同的行为。(不要做某些类型的假设)

小结

  • C++ 中有静态类型和动态类型概念
  • 利用多态能够实现对象的动态类型识别
  • typeid 是专用于类型识别的关键字
  • typeid 能够返回对象的动态类型信息

以上内容参考狄泰软件学院系列课程,请大家保护原创

脚本宝典总结

以上是脚本宝典为你收集整理的【C++】 66_C++ 中的类型识别全部内容,希望文章能够帮你解决【C++】 66_C++ 中的类型识别所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。