From: kotula@camax.com (Jeff Kotula)
Subject: Virtual base classes summary
Organization: CAMAX Systems, Inc.
Date: Mon, 14 Dec 1992 21:29:48 GMT

Here is a summary of the responses to my post a few weeks ago about virtual
base classes.  Good stuff!  Thanks for the responses...


From pat@frumious  Thu Dec  3 06:52:22 1992
Date: 	Wed, 2 Dec 1992 18:45:13 -0500
From: pat@frumious (Patrick Smith)
To: kotula@camax.com
Subject: Re: Virtual base classes

You wrote:
|Me and this other guy (who shall remain nameless) have been trying to
|figure out the appropriate use for virtual base classes.  Given the
|following inheritance hierarchy:
|        A
|       / \
|       B C
|       | |
|       D E
|       \/
|        F
|
|where we only want one copy of A, which set of inheritances should be
|virtual?
|
|    1) A to B  and  A to C

This one.



|    2) D to F  and  E to F

This won't work; if you do (2) but not (1), then every D object will
contain its own A subobject (not sharable with any other A object),
and every E object will contain its own A subobject (also not sharable).
So in an F you will have a D subobject (containing one A object)
and an E subobject (containing a different A object).

After (2), if you then created a new class,

   class G : public F, public virtual D {};

then G would only contain one D subobject (because of virtual
inheritance).

-- 
Patrick Smith
uunet.ca!frumious!pat
pat%frumious.uucp@uunet.ca

From jem@vuse.vanderbilt.edu  Wed Dec  2 18:51:40 1992
Date: Wed, 2 Dec 92 18:01:02 CST
From: jem@vuse.vanderbilt.edu (John Eric Meyer)
To: kotula@camax.com
Subject: Re: Virtual base classes
Organization: Vanderbilt University School of Engineering, Nashville, TN, USA
Cc: 

   A
  / \
 B   C
 |   |
 D   E
  \ /
   F

As I understand the nature of the beast, A to B and A to C must be virtual,
while D to F and E to F need not be.  Declaring D to F and E to F as virtual
*without* virtualizing A will not have the desired effect.  The virtual
state of inherited objects applies only to the object inherited as virtual,
not to the object and all its ancestry.

   A
   |
   B
  / \
 C   D
  \ /
   E

In this case 

   class B : public A;
   class C : virtual public B;
   class D : virtual public D;
   class E : public C, public D;

would result in E containing only one copy of B and one copy of A (within B, 
if you will).  A is in a sense virtualized by association. On the other hand

   class B : virtual public A;
   class C : public B;
   class D : public B;
   class E : public C, public D;

would result in E containing two copies of B but just one copy of A.

Hope this helps ( moreover, I hope this it correct! )

Eric
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
As I tell my friends, never listen to me when I'm wrong.
---------------------------------------------------------------------------

From landauer@Eng.Sun.COM  Thu Dec  3 06:52:28 1992
Date: Wed, 2 Dec 92 20:24:29 PST
From: landauer@Eng.Sun.COM (Doug Landauer)
To: kotula@camax.com
Subject: Re: Virtual base classes
Organization: Sun Microsystems, Mt. View, Ca.

> 	  A
>        / \
>        B C
>        | |
>        D E
>        \/
> 	  F
> 
> where we only want one copy of A, which set of inheritances should be
> virtual?
> 
>     1) A to B  and  A to C
> 	   or
>     2) D to F  and  E to F
> 
> I suspect that either will work, but which is best, and why?

No, only the #1 choice is appropriate.  One way to state the rule is to
say that if you want a base class to be shared, its closest derived
class must inherit it virtually.  Using the #2 choice, there will be
two A's.

Another way to look at it is the cfront way:  think of the most likely
implementation, and it will show you the semantics.  In this case,
virtual inheritance means a pointer instead of inclusion.  To share a
base class, we must have a pointer to *that* base class.  In your #2
choice above, we have only pointers to D and to E -- we cannot share
those.  We do *not* have any pointer to any A.

>  3) Are there hidden gotchas?

One gotcha is the one-way nature of virtual base class pointers.  For
instance, in one common OOP idiom, you have a class which implements
"list-of-ptr-to-base-class", and you populate that list with ptrs to
various derived classes, and upon extraction from the list, you
downcast the pointer to the type you happen to know it really is.
This idiom works fine *only in the absence* of virtual inheritance.
Because the pointer is one-way, you *cannot* cast a ptr-to-base into
being a ptr-to-derived if the inheritance is virtual.

One other gotcha is in debugging:  if your debugger's C++ support is
weak, you must understand this virtual-base-pointer business, else
the bits in memory make no sense.  (Sun's dbx support is pretty
reasonable in this respect.)
-- 
Doug Landauer -- landauer@eng.sun.com               _
Sun Microsystems, Inc. -- STE, SunPro, IPE         La no ka 'oi.

From nikki@trmphrst.demon.co.uk  Tue Dec  8 14:57:49 1992