When is static cast safe when you are using multiple inheritance?

前端 未结 6 597
眼角桃花
眼角桃花 2020-12-10 12:26

I found myself in a situation where I know what type something is. The Type is one of three (or more) levels of inheritance. I call factory which returns B* how

6条回答
  •  一整个雨季
    2020-12-10 13:03

    A cross cast doesn't need a dynamic_cast at all..

       struct Base1 { virtual void f1(); };
       struct Base2 { virtual void f2(); };
       struct Derived : Base1, Base2 {};
    
       Base1* b1 = new Derived();
    
       // To cast it to a base2 *, cast it first to a derived *
       Derived *d = static_cast(b1);
       Base2 *b2 = static_cast(d);
    

提交回复
热议问题