程序员

【c++11关键字】dynamic_cast

2020-10-21  本文已影响0人  小鱼号的代码日记
/*
 * c++11关键字
 * dynamic_cast
 * 小鱼号的代码日志
*/
#include <QCoreApplication>
#include <iostream>
using namespace  std;
struct Base
{
    virtual ~Base()
    {}
};
struct Derived :Base
{
    virtual void name()
    {

    }
};
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    Base *b1 = new Base;
    if(Derived *d = dynamic_cast<Derived *>(b1))
    {
        cout <<"down cast b1 to d successful";
    }
    Base *b2 = new Derived;
    if(Derived *d = dynamic_cast<Derived *>(b2))
    {
        cout <<"down cast b2 to d successful";
    }
    Base bb;
    Derived& cc = dynamic_cast<Derived&>(bb);  ///bad
    return a.exec();
}

上一篇 下一篇

猜你喜欢

热点阅读