Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Diamond shaped polymorphic Inheritance: sizeof Most derived Class

I understand that the Diamond shaped inheritance causes ambiguity and it can be avoided by using inheritance through virtual Base Classes, the question is not about it. The question is about sizeof the most derived class in a diamond shaped hierarchy when the classes are polymorphic. Here is a sample code and the sample output:

#include<iostream>

using namespace std;

class Base
{
    public:
        virtual void doSomething(){}  
};

class Derived1:public virtual Base
{
    public:
       virtual void doSomething(){}
};

class Derived2:public virtual Base
{
    public:
       virtual void doSomething(){}
};

class Derived3:public Derived1,public Derived2
{
    public:
       virtual void doSomething(){}
};

int main()
{
    Base obj;
    Derived1 objDerived1;
    Derived2 objDerived2;
    Derived3 objDerived3;

    cout<<"\n Size of Base: "<<sizeof(obj);
    cout<<"\n Size of Derived1: "<<sizeof(objDerived1);
    cout<<"\n Size of Derived2: "<<sizeof(objDerived2);
    cout<<"\n Size of Derived3: "<<sizeof(objDerived3);

    return 0;
}

The output i get is:

 Size of Base: 4
 Size of Derived1: 4
 Size of Derived2: 4
 Size of Derived3: 8

As I understand Base contains a virtual member function and hence,
sizeof Base = size of vptr = 4 on this environment

Similar is the case Derived1 & Derived2 classes.

Here are my questions related to above scenario:
How about size of a Derived3 class object, Does it mean Derived3 class has 2 vptr?
How does the Derived3 class work with these 2 vptr, Any ideas about the mechanism it uses?
The sizeof classes is left as implementation detail of compiler & not defined by the Standard(as the virtual mechanism itself is an implementation detail of compilers)?

like image 897
Alok Save Avatar asked Jan 20 '23 02:01

Alok Save


2 Answers

Yes, Derived3 has two vtable pointers. If you're accessing it by value, it uses the Derived3 version, or picks a function from a parent, or denotes that it's ambiguous if it can't decide.

In the case of a child, it uses the vtable corresponding to the parent 1/2 that's being used polymorphically.

Note that you didn't use virtual inheritance correctly: I believe Derived1 and 2 should inherit virtually from Base. sizeof(Derived3) still seems to be 8, because it still has two possible parents that could be treated as a Derived3. When you cast up to one of the parents the compiler will actually adjust the object pointer to have the correct vtable.

Also I should point out that anything vtable-related is implementation specific because there isn't even any mention of vtables in the standard.

like image 119
Mark B Avatar answered Feb 10 '23 02:02

Mark B


A small fix to your code: the virtual is supposed to be in the definition of derived2 and derived 3 in order to work.

http://www.parashift.com/c++-faq-lite/multiple-inheritance.html#faq-25.9

like image 20
ManicQin Avatar answered Feb 10 '23 02:02

ManicQin