Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!uwm.edu!zaphod.mps.ohio-state.edu!usc!rutgers!att!cbnewsm!stanl From: stanl@cbnewsm.ATT.COM (stanley.b.lippman) Newsgroups: comp.lang.c++ Subject: virtual base classes Message-ID: <8059@cbnewsm.ATT.COM> Date: 27 Dec 89 20:19:54 GMT Organization: AT&T Bell Laboratories Lines: 77 *** Shankar Unni writes: Consider the following situation: class A {public: int a_mem;}; class B : virtual public A {}; class C : public A {}; class D : public B, virtual public C {}; A A <<< ********** revised drawing / | virtual / | / | B C \ / \ / virtual \ / D Questions: (a) How many "A"'s are there in "D"? *****> two (b) If there is more than one, is the following access unambiguous, and if so, which "A" does it refer to: "D::a_mem" *****> no, it is ambiguous My copy of cfront (2.0) seems to think that: (a) there is only *one* A in D. *****> no, look again -- there are two, one within the C sub-object of D and the virtual OA instance: struct A { /* sizeof A == 4 */ int a_mem__1A ; }; struct B { /* sizeof B == 8 */ struct A *PA; struct A OA; }; struct C { /* sizeof C == 4 */ int a_mem__1A ; // members of A unrolled ... }; struct D { /* sizeof D == 16 */ struct A *PA; // members of B unrolled ... struct C *PC; struct C OC; // non-virtual instance of A within OC struct A OA; // virtual instance of A from B }; (b) (this is therefore moot). *****> cfront actually has a bug in that it is choosing the instance associated with the virtual object associated with B -- it should be ambiguous and will be fixed in 2.1 d . PA-> a_mem__1A = 1 ; // generated cfront stan lippman at&t bell laboratories warren, new jersey