The following code are from the book "Inside the C++ object model"
#include <iostream>  
using namespace std;
class X{};
class Y: public virtual X{};
class Z: public virtual X{};
class A: public Y, public Z{};
int main()
{
     cout<<sizeof(X)<<" "<<sizeof(Y)<<" "<<sizeof(Z)<<" "<<sizeof(A)<<endl;
     return 0;
}
In my computer(Windows, VS2010), the output is:
1 4 4 8
Here're my questions
1, sizeof(X)=1
The book says when X type generate two instance, say xa and xb. the compile insert a byte into A so that xa and xb can have different address. I'm not quite understand the reasons.
2, sizeof(Y)=4
By using virtual inheritance, will we have an additional virtual pointer? I guess this might be different from virtual pointer in polymorphism. Can anyone give me the memory layout for Y?
Thank you!
 
        class Y size(4):
            +---
            0     | {vbptr}
            +---
            +--- (virtual base X)
            +---
      Y::$vbtable@:
       0    | 0
       1    | 4 (Yd(Y+0)X)    
  vbi:     class  offset o.vbptr  o.vbte fVtorDisp
        X       4       0       4     0
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With