4
votes

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)?

4
regarding the Standard question. Yes the mechanism of how to implement virtual methods is an implementation details and not specified. Yes the actual result of sizeof is an implementation details too, it notably depends on pointer size, if you were on a 64 bits platform, you would be seeing 8/8/8/16.Matthieu M.

4 Answers

4
votes

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.

3
votes

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

1
votes

I think you are wondering about something that is totally implementation specific. You should not assume anything about the size of the classes.

Edit: though being curious is a proven quality ;-)

1
votes

Consider a slightly different case:

struct B { virtual void f(); };
struct L : virtual B { virtual void g(); };
struct R : virtual B { virtual void h(); };
struct D : L, R {};

In a typical implementation, L::g will be in the same position (say at index 0) in L's vtable as R:h in R's vtable. Now consider what happens given the following code:

D* pd = new D;
L* pl = pd;
R* pr = pd;
pl->g();
pr->h();

In the last two lines, the compiler will generate code to find the address of the function at the same position in the vtable. So the vtable accessed through pl cannot be the same as the one (or a prefix of the one) accessed through pr. Thus, the complete object needs at least two vptr, to point to two different vtable.