On 9/20/12 8:59 AM, lieve again wrote:
So, sizeof(Base1) == sizeof(Base2) == sizeof(NormalInheritance) == 4
bytes (only one virtual pointer)
but sizeof(MultipleInheritance) == 8 bytes
if class MultipleInheritance would inherit from another Base3, the
size would be 12 bytes and so on.
And this should be expected as we would expect
sizeof(MultipleInheritance) = sizeof(Base1) + sizeof(Base2) +
sizeof(stuff added in MultipleInheritance)
as we would normally expect that each of the base classes be fully
represented within the derived class so it is easy to treat the derived
class as if it was any of its base classes. (There is an exception for
empty base classes which must have a sizeof > 0 as a class by itself,
but might not take any extra room when derived from.)
So with multiple inheritance we ends with big classes because of the
need of extra virtual pointers,
to avoid that, almost every language doesn't implement multiple
inheritance but Interfaces, where one
can only inherit more than one class but being that classes abstract
or pure virtual, like:
class Base1{
virtual void func() = 0;
virtual void func3();
};
class MixedClass : public NormalClass implements Base1, Base2,
Base...
My question is: Don't we have the same implementation problem as in C+
+? Because even being these classes
abstract, they need a virtual pointer. Why do they impose that rule
in languages like C#, Java, D...?
Someone know the reason?
When inheriting from Interfaces, the difference is that the Interface
never needs to exist as a discrete object, so there isn't a need to save
a vtable pointer for each Interface. The Interface routine likely need a
pointer into the base class vtable to the vtable for that interface, but
that should be computable from the object normal vtable pointer.
Bs.As.