zoukankan      html  css  js  c++  java
  • 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
    
  • 相关阅读:
    UGUI组件之Scrollbar 卷动条组件简单笔记
    在VerilogHDL中调用VHDL的模块
    jQuery表格删除功能
    offset方法与position方法
    小火箭返回顶部案例
    jQuery弹幕效果
    用jQuery写微博发布案例
    用jQuery写城市选择案例
    jquery创建与添加节点
    jQuery停止动画详情
  • 原文地址:https://www.cnblogs.com/wuchanming/p/3758870.html
Copyright © 2011-2022 走看看