C++3种动态类型识别的方法

一,什么是动态类型

基类指针所指对象的实际类型

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

二,方法

1,利用多态

  1. 必须从基类开始提供类型虚函数
  2. 所有的派生类都必须重写类型虚函数
  3. 每个派生类的类型ID必须唯一

 

2,利用dynamic_cast

dynamic_cast这个关键字如果要转换的实际类型和指定的类型不一样,则会返回NULL。例如当指定类型为子类对象时,如果父类指针的动态类型是这个子类对象时,没有错误,而动态类型是父类对象或者其他子类对象时 错误

  1. dynamic_cast要求使用的目标对象类型是多态的,即:所在类族至少有一个虚函数
  2. 只能用于指针和引用之间
  3. 用于指针转换时,转换失败返回空指针
  4. 用于引用转换时,转换失败将引发bad_cast异常

 

 

3,利用typeid(推荐这种方法)

typdeid 专门用于动态类型识别

  1. 它是一个关键字
  2. typeid返回一个type_info类对象
  3. 使用时需要包含头文件<typeinfo>
  4. 直接指定对象或者类型,普通类型普通对象也可以

 

 

三,使用例子

a,多态

class Parent
{
public:
    enum { ID = 0 };
    
    virtual int type()
    {
        return ID;
    }
};

class Child : public Parent
{
public:
    enum { ID = 1 };
    
    int type()
    {
        return ID;
    }
    
    int add(int a, int b)
    {
        return a + b;
    }
};

void test(Parent* p)
{
    if( p->type() == Child::ID )
    {
        Child* c = (Child*)p;
        
        cout<<"Dynamic Type: "<<"Child"<<endl;
        cout<<"add: "<<c->add(2, 3)<<endl;
    }
    
    if( p->type() == Parent::ID )
    {
        cout<<"Dynamic Type: "<<"Parent"<<endl;
    }
}

int main(int argc, char *argv[])
{
    Parent parent;
    Child child;
    
    test(&parent);
    test(&child);
    
    cout << "Press the enter key to continue ...";
    cin.get();
    return EXIT_SUCCESS;
}

 

 b,dynamic_cast

#include <cstdlib>
#include <iostream>

using namespace std;

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

class Child : public Parent
{
public:
    int add(int a, int b)
    {
        return a + b;
    }
};

class NewChild : public Parent 
{
};

void test(Parent* p)
{
    Child* c = dynamic_cast<Child*>(p);//p的动态类型是 非Child对象的话会返回NULL 
    
    if( c != NULL )
    {
        cout<<"Dynamic Type: "<<"Child"<<endl;
        cout<<"add: "<<c->add(2, 3)<<endl;
    }
    else
    {
        if( dynamic_cast<NewChild*>(p) != NULL )
        {
            cout<<"Dynamic Type: "<<"NewChild"<<endl;
        }
        else
        {
            cout<<"Dynamic Type: "<<"Parent"<<endl;
        }
    }
}

int main(int argc, char *argv[])
{
    Parent parent;
    Child child;
    NewChild nc;
    
    test(&parent);
    test(&child);
    test(&nc);
    
    cout << "Press the enter key to continue ...";
    cin.get();
    return EXIT_SUCCESS;
}

c,typeid

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

class Child : public Parent
{
public:
    int add(int a, int b)
    {
        return a + b;
    }
};

class NewChild : public Parent 
{
};

void test(Parent* p)
{
    if( typeid(*p) == typeid(Child) )
    {
        Child* c = dynamic_cast<Child*>(p);
    
        cout<<"Dynamic Type: "<<"Child"<<endl;
        cout<<"add: "<<c->add(2, 3)<<endl;
    }
    else if( typeid(*p) == typeid(NewChild) )
    {
        cout<<"Dynamic Type: "<<"NewChild"<<endl;
    }
    else if( typeid(*p) == typeid(Parent) )
    {
        cout<<"Dynamic Type: "<<"Parent"<<endl;
    }

}

int main(int argc, char *argv[])
{
    Parent parent;
    Child child;
    NewChild nc;
    int index;
    char ch;
    
    const type_info& tp = typeid(parent);
    const type_info& tc = typeid(child);
    const type_info& tn = typeid(nc);
    const type_info& ti = typeid(index);
    const type_info& tch = typeid(ch);
    
    cout<<tp.name()<<endl;
    cout<<tc.name()<<endl;
    cout<<tn.name()<<endl;
    cout<<ti.name()<<endl;
    cout<<tch.name()<<endl;
    
    test(&parent);
    test(&child);
    test(&nc);
    
    cout << "Press the enter key to continue ...";
    cin.get();
    return EXIT_SUCCESS;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值