C++ pointer multi-inheritance fun

后端 未结 5 1526
自闭症患者
自闭症患者 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:55

    • does pa point to a valid A object?
    • does pb point to a valid B object?

    Yes, the C* gets converted so that pa and pb point to the correct addresses.

    • does pa == pb ?

    No, usually not. There can't be an A object and a B object at the same address.

    Furthermore, does

    • pc == (C*) pa ?
    • pc == (C*) pb ?

    The cast converts the pointers back to the address of the C object, so both equalities are true.

提交回复
热议问题