Double abstract class Inheritance concern.

Nathan Sidwell nathan@codesourcery.com
Mon Mar 29 17:16:00 GMT 2004


Chris Lattner wrote:
> Tiago Stein wrote:
> 
>>class A{};
>>class B :public A{};
>>class C: public A{ int g; };
>>class D: public B, public C {
>>  unsigned int foo;
>>};
>>Why sizeof class D is 12? Should not be 8?
> 
> 
> I believe the reason is that the ABI does not permit multiple different
> instances of A to be located at the same address, even if they have zero
> size.  If the size of the structure was 8, all of "A", "B", and "A" would
> have offset zero.
> 
> I'm not sure WHY this is required, but this is the reason it happens at
> least.  A declaration of "A X[100];" allocates 100 bytes as
> a result of the same rule.

It is a C++ language requirement that no two logically distinct objects
of the same type have the same address.

BEWARE. GNU C has an empty struct extension to C, that does not obey
the same rules.

nathan

-- 
Nathan Sidwell    ::   http://www.codesourcery.com   ::     CodeSourcery LLC
nathan@codesourcery.com    ::     http://www.planetfall.pwp.blueyonder.co.uk




More information about the Gcc mailing list