Reputation: 778
Regarding the size of the derived class, is it better to have a "chain" of inheritance or inherit everything in the lowest derived class?
for example, what is better between:
class Base {
virtual void something() = 0;
};
class Derived1 {
// ...
};
class Derived2 : public Derived1, public Base {
// ...
};
and
class Base {
virtual void something() = 0;
};
class Derived1 : public Base {
// ...
};
class Derived2 : public Derived1 {
// ...
};
In the second case, will it have to store two vtable pointers and just one in the first case?
In the first case, I have a sizeof(Derived)
lower that in the second case.
Upvotes: 1
Views: 291
Reputation: 1692
What you're seeing as far as i can see is simply an empty base class optimization.
See the godbolt I've prepared. When you don't have empty classes, the sizes are identical.
To specifically address your question Regarding the size of the derived class, is it better to have a "chain" of inheritance or inherit everything in the lowest derived class?
I guess if you have an empty base class in there, you could claim it to be "better" in terms of size of the class, but that is pretty dependent on the specifics of the way the class is defined that could change at any time.
I wouldn't base an inheritance structure on it unless i was really pressed for space.
Upvotes: 1