I was recently asked in an interview about object layout with virtual functions and multiple inheritance involved.
I explained it in context of how it is implemented without multiple inheritance involved (i.e. how the compiler generated the virtual table, insert a secret pointer to the virtual table in each object and so on).
It seemed to me that there was something missing in my explanation.
So here are questions (see example below)
- What is the exact memory layout of the object of class C.
- Virtual tables entries for class C.
- Sizes (as returned by sizeof) of object of classes A, B and C. (8, 8, 16 ?? )
- What if virtual inheritance is used. Surely the sizes and virtual table entries should be affected ?
Example code:
class A {
public:
virtual int funA();
private:
int a;
};
class B {
public:
virtual int funB();
private:
int b;
};
class C : public A, public B {
private:
int c;
};
Thanks!
See Question&Answers more detail:os