Path: utzoo!attcan!uunet!cs.utexas.edu!ut-emx!nowhere!sking From: sking@nowhere.uucp (Steven King) Newsgroups: comp.lang.c++ Subject: Re: Multiple inheritance with virtual bases Message-ID: <1990Oct21.211143.3459@nowhere.uucp> Date: 21 Oct 90 21:11:43 GMT Organization: nowhere Lines: 41 In article hagiwara@zuken.co.jp (Kazuyuki Hagiwara) writes: > >I am using AT&T C++ version 2.0. Following is the simple test of >multipile inheritance with virtual base classes. Do anyone know >the reason why sizeof(D) is so large ? I think sizeof(D) should be >only 1000 bytes and some pointer overhead. > >----------------------------- mibug.C { ---------------- >#include > >struct A { > char avar0[1000]; >}; > >struct B : virtual public A { > int bvar; >}; > >struct C : virtual public A { > int cvar; >}; > >struct D : virtual public B, virtual public C { > int dvar; >}; The definitions of B and C each have space for A allocated within them; thus the size of D is sizeof B ( which is sizeof A + pointer overhead ) + sizeof C ( sizeof A + pointer overhead ) + more pointer overhead. A little thought on how virtual bases are implemented should reveal why this is, tho' I'm not sure that a smarter Cfront might only need to alloc space for it once. -- sking@nowhere | C++ is to C, what espresso is to coffee; ..!cs.utexas.edu!ut-emx!nowhere!sking | Definitely an acquired taste, but once | you have acquired that taste, you cant | go back ...