C++ pointer multi-inheritance fun

后端 未结 5 1517
自闭症患者
自闭症患者 2020-11-30 09:02

I\'m writing some code involving inheritance from a basic ref-counting pointer class; and some intricacies of C++ popped up. I\'ve reduced it as follows:

Suppose I h

5条回答
  •  粉色の甜心
    2020-11-30 09:47

    C embeds an A and a B.

    class C: public A, public B {};
    

    is very similar to the C code

    struct C {
        A self_a;
        B self_b;
    };
    

    and (B*) &c; is equivalent to static_cast< B* >( &c ) is similar to &c.self_b if you were using straight C.

    In general, you can't rely on pointers to different types being interchangeable or comparable.

提交回复
热议问题