• C++--第27课


    第27课 - 动态类型识别

    问题:下面的程序有问题吗?

    class Parent

    {

    public:

    virtual -Parent()

    {

    }

    };

    class Child : public Parent

    {

    };

    void test(Parent* p)

    {

    Child* c = (Child*)p;  //将父类强制转化为子类

    }

    1. 动态类型

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

    (p指向的静态类型为Parent)Parent* p = new Child();(p实际指向的对象类型为Child)

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

    void test(Parent* p)

    {

    Child* c = (Child*)p; //当p的动态类型为Child时,转换成功

                    //否则,可能出现无法预知的错误

    }

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

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

    2. 动态类型识别

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

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

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

    (3)      每个类中的类型虚函数都需要不同的实际。

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

    #include <cstdlib>

    #include <iostream>

    using namespace std;

    class Parent

    {

    public:

        enum { ID = 0 };

        virtual int type()  //自动变成虚函数,类型虚函数

        {

            return ID;

        }

    };

    class Child : public Parent

    {

    public:

        enum { ID = 1 };

        int type()      //自动变成虚函数virtual,类型虚函数

        {

            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;

    }

    运行结果:

    Dynamic Type: Parent

    Dynamic Type: Child

    add: 5

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

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

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

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

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

    改进:

    在头文件中加入#include<string>,虚函数中返回return “Child”;return “Parent”;在后面的判断语句中,用if(strcmp(p->type90,”Child”)==0)来判断,就会得到我们上面一样的结果。但是这里面的维护性好了,但是效率会变得更低,所以这种方式,也不好。也就是凡是使用多态就会出现问题。个人倾向于使用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()

        {

        }

    };

    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);

        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;

    }

    运行结果:

    Dynamic Type: Parent

    Dynamic Type: Child

    add: 5

    Dynamic Type: NewChild

    dynamic_cast是优势

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

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

    dynamic_cast的缺陷    

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

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

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

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

    typeid关键字返回对应参数的类型信息。这是专门为动态类型识别提供的关键字。

    typeid返回一个type_info类对象:当typied的参数为NULL时,抛出bad_typied异常。

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

    class type_info

    {

    public:

    virtual ~type_info();

    bool operator==(const type_info& rhs);

    bool operator!=(const type_info& rhs);

    int before(const type_info& rhs);

    const char* name();

    };

    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) )

        {

            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;

    }

    运行结果:

    6Parent

    5Child

    8NewChild

    i

    c

    Dynamic Type: Parent

    Dynamic Type: Child

    add: 5

    Dynamic Type: NewChild

    分析:

    typeid是一个与编译器实现相关的关键字,在有的编译器中是不支持的。我们看到的结果在不同的编译器中是不一样的。

    小结:

    C++可以通过多态的方式进行动态类型识别。

    dynamic_cast关键字是可用动态类型识别。

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

  • 相关阅读:
    JVM理论:(一/2)OutOfMemoryError异常
    JVM理论:(一/1)对象的创建过程
    JVM理论:(一)JVM内存模型
    MySQL优化(6):Mysql锁机制
    MySQL优化(5):索引失效分析、in与exists使用场合
    MySQL优化(4):explain分析
    MySQL优化(3):慢SQL分析
    MySQL优化(2):索引简述
    MySQL优化(1):Mysql简述
    MySQL基础(4):事务控制
  • 原文地址:https://www.cnblogs.com/free-1122/p/11336323.html
Copyright © 2020-2023  润新知