Path: utzoo!attcan!uunet!zaphod.mps.ohio-state.edu!usc!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: <1990Oct27.041220.9150@nowhere.uucp> Date: 27 Oct 90 04:12:20 GMT Organization: nowhere Lines: 91 In article <488@taumet.com> steve@taumet.com (Stephen Clamage) writes: >sking@nowhere.uucp (Steven King) writes: > >|In article |HAGIWARA.90Oct18105013@rd14.zuken.co.jp> hagiwara@zuken.co.jp (Kazuyuki Hagiwara) writes: >|> >[ ... ] >|>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. > >This is not correct. Exactly one instance of a virtual base class exists >in an object by definition. Class A is declared to be virtual in all uses >of it, so it is an error for there to be more than one copy. Let us hope >that the error in this case is in the allocation, and that only one copy >is ever referred to by generated code. ( sigh ) It is correct as a simple test and examination of the C output would have shown. To wit: struct A { /* sizeof A == 1000 */ char avar0__1A [1000]; }; struct B { /* sizeof B == 1008 */ int bvar__1B ; struct A *PA; struct A OA; }; struct C { /* sizeof C == 1008 */ int cvar__1C ; struct A *PA; struct A OA; }; struct D { /* sizeof D == 2032 */ int dvar__1D ; struct C *PC; struct B *PB; struct A *PA; struct C OC; struct B OB; }; Each class has space allocated for the virtual base, and has a pointer to the virtual base it actually uses. The constructors are passed a pointer to the virtual base they are to use. Tf the pointer is NULL, they initialize it, call the constructor for the virtual base, and pass it to the constructors for the classes they are derived from. Its pretty straight forward, tho' the code generated can be rather dense.... and now for a slight flame.... Am I the only person using Cfront who checks the C output when there is a question about what it is doing? If one is using Cfront and has a question about what is going on (especially in one's own code), there is no excuse for not examining the C output. If the lack of formatting bothers you, then run it thru cb ( I edited my CC script to do it automatically for me ) Or are there a bunch of C weenies out there who cant stand the sight of raw C code ;-} regards, -- sking@nowhere | C++ is to C, what espresso is to ..!cs.utexas.edu!ut-emx!nowhere!sking | coffee; Definitely an acquired taste, | but once you have acquired that taste, | you cant go back ...