第27节 动态类型识别

-------------------------------------资源来源于网络,仅供自学使用,如有侵权,联系我必删.

第一:

问题

下面的程序有问题吗?

出问题情况:当使用一个父类对象调用test()函数时,可能会出错!

 

由于基类指针可以直接指向派生类对象,因此可能存在指针所指类型具体指向的对象类型不同的情况

 

动态类型指的是基类指针指向对象的实际类型

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

 

第二:

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

C++中的多态根据实际的对象类型调用对应的虚函数

1.可以在基类中定义虚函数返回具体的类型信息

2.所有的派生类都必须实现类型相关的虚函数

3.每个类中的类型虚函数都需要不同的实现

 

利用多态进行动态类型识别

#include <cstdlib>
#include <iostream>

using namespace std;

class Parent//基类
{
public:
    enum { ID = 0 };//定义枚举常量,父类ID值为0
    
    virtual int type()//定义虚函数,用于动态类型识别
    {
        return ID;
    }
};

class Child : public Parent//定义派生类Child
{
public:
    enum { ID = 1 };//定义枚举常量,子类ID值为1
    
    int type()//重写虚函数type()
    {
        return ID;
    }
    
    int add(int a, int b)
    {
        return a + b;
    }
};

//用于实现动态类型识别
void test(Parent* p)
{
    if( p->type() == Child::ID )//用于p指针判断是父类ID还是子类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);//父类ID
    test(&child); //子类ID
    
    cout << "Press the enter key to continue ...";
    cin.get();
    return EXIT_SUCCESS;
}

使用虚函数进行动态类型识别的缺陷

1.必须从基类开始提供类型虚函数

2.所有的派生类都必须重写类型虚函数

3.每个派生类的类型ID必须唯一

利用虚函数进行动态类型识别的方法可以满足工程的需要,但是维护性会随着派生类的增多而成指数级增加。

 

第三:

新的关键字dynamic_cast 实现动态类型识别

1.dynamic_cast 是C++中的新型转换关键字

2.dynamic_cast 主要用于基类和派生类之间的转换

3.dynamic_cast  要求使用的目标对象类型是多态的

即:所在类族至少有一个虚函数

用于指针转换时,转换失败返回空指针

用于引用转换时,转换失败将引发bad_cast异常

 

利用dynamic_cast进行动态类型识别

 

#include <cstdlib>
#include <iostream>

using namespace std;

class Parent
{
public:
    //所在类族至少有一个虚函数 
    virtual ~Parent()//dynamic_cast关键字要求对象里面有虚函数,则析构函数定义为虚函数
    {
    }
};

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);
  
    //用于指针转换时,转换失败返回空指针 
    //用于引用转换时,转换失败将引发bad_cast异常  
    if( c != NULL )//转换成功说明指向基类Parent*的指针 和 指向派生类Child*类型的p指针 相同
    {
        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;
}

dynamic_cast的优势

  不用显示的声明和定义类型虚函数

  不用为类族中的每个类分配类型ID

dynamic_cast的缺陷

  只能用于具有虚函数的类族

 

 使用dynamic_cast进行动态类型识别可以取代类型虚函数的方案,但是在本质上dynamic_cast还是需要类族中存在虚函数(在工程上常把析构函数作为这个虚函数定义).

 

第四

C++中是否可以动态得到任意变量的类型信息呢?

 

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

1.typeid关键字返回对应参数的类型信息

2.typeid返回一个type_info类对象

    当typeid的参数为NULL时,抛出bad_typeid异常

3.type_info类的使用需要包含<typeinfo> 

 

typeid关键字的使用

#include <cstdlib>
#include <iostream>
#include <typeinfo>//动态类型识别头文件

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)
{
    if( typeid(*p) == typeid(Child) )//p所指向对象的类型信息和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) )//p所指向对象的类型信息和NewChild对象一样
    {
        cout<<"Dynamic Type: "<<"NewChild"<<endl;
    }
    else if( typeid(*p) == typeid(Parent) )//p所指向对象的类型信息和Parent对象一样
    {
        cout<<"Dynamic Type: "<<"Parent"<<endl;
    }

}

int main(int argc, char *argv[])
{
    //4个类型的对象
    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.C++中可以通过多态的方式进行动态类型识别

2.dynamic_cast关键字是可用于动态类型识别

3.typeid关键字在C++中专用于动态类型的识别

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值