• dynamic_cast


    #include <typeinfo>
    using std::bad_cast;
    
    #include <iostream>
    using std::cout; using std::endl;
    
    struct A { virtual ~A() { } };
    struct B : virtual public A { /* . . . */ };
    struct C : public B { /* . . . */ };
    struct D : public B, virtual public A { /* . . . */ };
    
    void exercises() {
        A *pa = new C;
        if (B *pb = dynamic_cast< B* >(pa))
             cout << "cast from C to B* ok" << endl;
        else
             cout << "cast from C to B* not ok" << endl;
        B *pb = new B;
        if (C *pc = dynamic_cast< C* >(pb))
             cout << "cast from B to C* ok" << endl;
        else
             cout << "cast from B to C* not ok" << endl;
    
        A *pc = new C;
        if (B *pb = dynamic_cast< B* >(pc))
             cout << "cast C to B* ok" << endl;
        else
             cout << "cast C to B* not ok" << endl;
    
        A *pd = new D;
        if (B *pb = dynamic_cast< B* >(pd))
             cout << "cast D to B* ok" << endl;
        else
             cout << "cast D to B* not ok" << endl;
    }
    
    struct Base {
       virtual ~Base() {};
    };
    
    struct Derived: public Base { };
    
    void cast_to_ref(const Base &b)
    {
        try {
            const Derived &d = dynamic_cast<const Derived&>(b);
        cout<<"success"<<endl;
        // use the Derived object to which b referred
        } catch (bad_cast) {
            cout << "called f with an object that is not a Derived" << endl;
        }
    }
    
    int main()
    {
        Base *bp; 
        bp = new Derived;  // bp actually points to a Derived object
        if (Derived *dp = dynamic_cast<Derived*>(bp)) 
        {
            // use the Derived object to which dp points
        } else {  // bp points at a Base object
            // use the Base object to which bp points
        }
    
        exercises();
        
        cast_to_ref(*bp);
        cast_to_ref(Base());
    }

    j结果如下:

    cast from C to B* ok
    cast from B to C* not ok
    cast C to B* ok
    cast D to B* ok
    success
    called f with an object that is not a Derived
    
  • 相关阅读:
    js --- for in 和 for of
    vue -- config index.js 配置文件详解
    vue -- 脚手架之webpack.dev.conf.js
    vue --- 解读vue的中webpack.base.config.js
    vue 引入第三方字体包
    js call 和 apply
    vue2.0中的$router 和 $route的区别
    懒加载js实现和优化
    vue的指令在webstrom下报错
    BFC的布局规则和触发条件
  • 原文地址:https://www.cnblogs.com/wuchanming/p/3758870.html
Copyright © 2020-2023  润新知