C++.运行时类型判断_测试代码

ZC:C++ 编程思想——运行时类型识别 - 浅墨浓香 - 博客园.html(https://www.cnblogs.com/5iedu/articles/5585895.html

------------------------------两种Bad-cast-----------------------------------
1. dynamic_cast转换一个完全不相关的类
2. typeid操作一个空指针

1、环境:Win7x64、Qt5.3.2 MSVC2010 OpenGL、vs2010

2、代码:

    class Tbase
    {
    public:
        int Fi;

    public:
        virtual void Say(){ qDebug() << "Tbase"; }
    };

    class Tother
    {
    public:
        int Fi;

    public:
        virtual void Say(){ qDebug() << "Tother"; }
    };

    class TA1 :public Tbase
    {
    public:
        int FiA;

    public:
        virtual void Say(){ qDebug() << "TA1"; }
    };

    class TB1 :public TA1
    {
    public:
        int FiB;

    public:
        virtual void Say(){ qDebug() << "TB1"; }
    };

    class TC1 :public TB1
    {
    public:
        int FiC;

    public:
        virtual void Say(){ qDebug() << "TC1"; }
    };


    class TA2 :public Tbase
    {
    public:
        int FiA;

    public:
        virtual void Say(){ qDebug() << "TA2"; }
    };

    class TB2 :public TA2
    {
    public:
        int FiB;

    public:
        virtual void Say(){ qDebug() << "TB2"; }
    };

    class TC2 :public TB2
    {
    public:
        int FiC;

    public:
        virtual void Say(){ qDebug() << "TC2"; }
    };

void MainWindow::on_pushButton_clicked()
{
    Tbase *pC1 = new TC1();

    if ( typeid(*pC1) == typeid(TC1) ) { qDebug() << "pC1 is TC1"; } else { qDebug() << "pC1 is not TC1"; }  // ZC: is
    if ( typeid(*pC1) == typeid(TB1) ) { qDebug() << "pC1 is TB1"; } else { qDebug() << "pC1 is not TB1"; }  // ZC: is not
    if ( typeid(*pC1) == typeid(TA1) ) { qDebug() << "pC1 is TA1"; } else { qDebug() << "pC1 is not TA1"; }  // ZC: is not
    if ( typeid(*pC1) == typeid(Tbase) ) { qDebug() << "pC1 is Tbase"; } else { qDebug() << "pC1 is not Tbase"; }  // ZC: is not
    qDebug() << "";

    if ( typeid(*pC1) == typeid(TC2) ) { qDebug() << "pC1 is TC2"; } else { qDebug() << "pC1 is not TC2"; }  // ZC: is not
    if ( typeid(*pC1) == typeid(TB2) ) { qDebug() << "pC1 is TB2"; } else { qDebug() << "pC1 is not TB2"; }  // ZC: is not
    if ( typeid(*pC1) == typeid(TA2) ) { qDebug() << "pC1 is TA2"; } else { qDebug() << "pC1 is not TA2"; }  // ZC: is not
    qDebug() << "";

  // ***

    Tbase *pB1 = new TB1();

    if ( typeid(*pB1) == typeid(TC1) ) { qDebug() << "pB1 is TC1"; } else { qDebug() << "pB1 is not TC1"; }  // ZC: is not
    if ( typeid(*pB1) == typeid(TB1) ) { qDebug() << "pB1 is TB1"; } else { qDebug() << "pB1 is not TB1"; }  // ZC: is
    if ( typeid(*pB1) == typeid(TA1) ) { qDebug() << "pB1 is TA1"; } else { qDebug() << "pB1 is not TA1"; }  // ZC: is not
    if ( typeid(*pB1) == typeid(Tbase) ) { qDebug() << "pB1 is Tbase"; } else { qDebug() << "pB1 is not Tbase"; }  // ZC: is not
    qDebug() << "";

    if ( typeid(*pB1) == typeid(TC2) ) { qDebug() << "pB1 is TC2"; } else { qDebug() << "pB1 is not TC2"; }  // ZC: is not
    if ( typeid(*pB1) == typeid(TB2) ) { qDebug() << "pB1 is TB2"; } else { qDebug() << "pB1 is not TB2"; }  // ZC: is not
    if ( typeid(*pB1) == typeid(TA2) ) { qDebug() << "pB1 is TA2"; } else { qDebug() << "pB1 is not TA2"; }  // ZC: is not
    qDebug() << "";

    qDebug() << "*** *** *** *** *** *** *** *** *** *** ***";

}

void MainWindow::on_pushButton_2_clicked()
{
    Tbase *pBase = new TC1();
    TC1* pC1 = dynamic_cast<TC1*>(pBase);
    TB1* pB1 = dynamic_cast<TB1*>(pBase);
    TA1* pA1 = dynamic_cast<TA1*>(pBase);
    qDebug() << "pC1" << (int)pC1;  // ZC: != 0
    qDebug() << "pB1" << (int)pB1;  // ZC: != 0
    qDebug() << "pA1" << (int)pA1;  // ZC: != 0

    TC2* pC2 = dynamic_cast<TC2*>(pBase);
    TB2* pB2 = dynamic_cast<TB2*>(pBase);
    TA2* pA2 = dynamic_cast<TA2*>(pBase);
    qDebug() << "pC2" << (int)pC2;  // ZC: == 0
    qDebug() << "pB2" << (int)pB2;  // ZC: == 0
    qDebug() << "pA2" << (int)pA2;  // ZC: == 0

    qDebug() << "";

    pBase = new TB1();
    pC1 = dynamic_cast<TC1*>(pBase);
    pB1 = dynamic_cast<TB1*>(pBase);
    pA1 = dynamic_cast<TA1*>(pBase);
    qDebug() << "pC1" << (int)pC1;  // ZC: == 0
    qDebug() << "pB1" << (int)pB1;  // ZC: != 0
    qDebug() << "pA1" << (int)pA1;  // ZC: != 0

    pC2 = dynamic_cast<TC2*>(pBase);
    pB2 = dynamic_cast<TB2*>(pBase);
    pA2 = dynamic_cast<TA2*>(pBase);
    qDebug() << "pC2" << (int)pC2;  // ZC: == 0
    qDebug() << "pB2" << (int)pB2;  // ZC: == 0
    qDebug() << "pA2" << (int)pA2;  // ZC: == 0

    qDebug() << "";

    pBase = new Tbase();
    Tother* pOther = dynamic_cast<Tother*>(pBase);
    qDebug() << "pOther" << (int)pOther;  // ZC: == 0

}

3、控制台输出:

  3.1、Debug:

  3.2、Release:

pC1 is TC1
pC1 is not TB1
pC1 is not TA1
pC1 is not Tbase

pC1 is not TC2
pC1 is not TB2
pC1 is not TA2

pB1 is not TC1
pB1 is TB1
pB1 is not TA1
pB1 is not Tbase

pB1 is not TC2
pB1 is not TB2
pB1 is not TA2

*** *** *** *** *** *** *** *** *** *** ***
pC1 4912920
pB1 4912920
pA1 4912920
pC2 0
pB2 0
pA2 0

pC1 0
pB1 4919680
pA1 4919680
pC2 0
pB2 0
pA2 0

pOther 0

4、

扫描二维码关注公众号,回复: 2790096 查看本文章

5、

猜你喜欢

转载自www.cnblogs.com/cppskill/p/9485523.html