C++ pointer multi-inheritance fun

后端 未结 5 1525
自闭症患者
自闭症患者 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 10:00

    What you get is something like this in memory

     ----------
     | A data |
     ----------
     | B data |
     ----------
     | C data |
     ----------
    

    So if you want the entire C object you'll get a pointer to the beginning of the memory. If you want only the A "part", you get the same address since that's where the data members are located. If you want the B "part" you get the beginning + sizeof(A) + sizeof(whatever the compiler adds for vtable). Thus, in the example, pc != pb (could be pc != pa) but pa is never equal to pb.

提交回复
热议问题