Vptr count in virtual inheritance

I was curious about the number of vptr in virtual inheritance. So I am writing the following code to get information about the number of vptrs.

class Base
{
    public: virtual void A() { std::cout<<"Base::A()"<<std::endl; }
};

class AnotherBase
{
    public: virtual void B() { std::cout<<"AnotherBase::B()"<<std::endl; }
};

class Child1 : public Base
{
    public: virtual void A() { std::cout<<"Child1::A()"<<std::endl; }
};

class Child2 : public Base
{
    public: virtual void B() { std::cout<<"Child2::B()"<<std::endl; }
};

class Child3 : public virtual Base
{

};

class Child4 : public virtual Base
{
    public: virtual void A() { std::cout<<"Child4::A()"<<std::endl; }
};

class Child5 : public virtual Base
{
    public: virtual void A() { std::cout<<"Child5::A()"<<std::endl; }
    public: virtual void B() { std::cout<<"Child5::B()"<<std::endl; }
};

class Child6 : public Base, public AnotherBase
{

};

class Child7 : public virtual Base, public AnotherBase
{

};

class Child8: public virtual Base, public virtual AnotherBase
{

};

int main() 
{
    using std::cout;
    cout<<"Base    :"<<sizeof(Base)<<std::endl;
    cout<<"Child1  :"<<sizeof(Child1)<<std::endl;
    cout<<"Child2  :"<<sizeof(Child2)<<std::endl;
    cout<<"Child3  :"<<sizeof(Child3)<<std::endl;
    cout<<"Child4  :"<<sizeof(Child4)<<std::endl;
    cout<<"Child5  :"<<sizeof(Child5)<<std::endl;
    cout<<"Child6  :"<<sizeof(Child6)<<std::endl;
    cout<<"Child7  :"<<sizeof(Child7)<<std::endl;
    cout<<"Child8  :"<<sizeof(Child8)<<std::endl;
    return 0;
 }

      

after running this code i found output as

/* On Visual Studio 2012 */ 
Base    :4
Child1  :4
Child2  :4
Child3  :8
Child4  :8
Child5  :12
Child6  :8
Child7  :12
Child8  :12

      

i invalid size Base, Child1 and Child2 are 4 because of vptr
why are Child3 and Child4 sizes 8? (I suspect this is a vptr due to virtual inheritance)
why is Child5 size 12? (I don't know)
why is Child6's size 8 (because of vptrs from both base classes)
why is Child7's size 12? (I guess 8 for two vptr's from both base classes each, an extra 4 due to virtual inheritance?)
Why is Child8 size 12? (I dont know)

please explain these questions.

Environment: 64-bit Windows 7. Visual Studio 2012

I am running this code in GCC 4.8.1, I found the output as ...

/* On GCC 4.8.1 (Online)*/
Base    :8
Child1  :8
Child2  :8
Child3  :8
Child4  :8
Child5  :8
Child6  :16
Child7  :16
Child8  :16

      

Now I am completely confused. Why is the base 8 bytes?

+3


source to share


2 answers


When we use virtual inheritance, there will be an overhead of 4 bytes (32-bit) or 8 bytes (64-bit) for a virtual base class pointer in that class.

The total size of each class depends on the compiler implementation.



More information can be found here:
http://www.codeproject.com/Articles/7150/Member-Function-Pointers-and-the-Fastest-Possible

0


source


I am running your code on 32 bit Windows 8.1 and cygwin gcc 4.8.1, here is the result:

Base    :4  
Child1  :4    
Child2  :4   
Child3  :4   
Child4  :4  
Child5  :4  
Child6  :8  
Child7  :8  
Child8  :8  

      



so maybe it's just a different implementation difference between different compilers

0


source







All Articles