[comp.lang.c++] Direct derivation of virtual base

randolph@ssd.kodak.com (Gary L. Randolph) (11/20/90)

The following hierarchy is set up by code found below:

                    X
               priv/|\priv
                  Y | Z
                   \|/
                   XYZ

Note that the derivations of Y and Z are private. BUT the virtual base is
derived *publicly* by XYZ.  Therefore, I would expect XYZ to inherit
public members directly through this path.  Please do NOT tell me about
the fact that pointer conversion from derived to base is not allowed due
to the private derivation, I know that and am not expecting conversion.

I have two questions:

     1.) Why is X a private base class to XYZ even though I publicly
         derive it for XYZ?

     2.) In general, what is gained from deriving a virtual base class 
         directly? Assuming all derivations above were public, what
         would an XYZ get that it wouldn't have from Y and Z anyway?


The following code results in the compile time error:
   "tst.C", line 31: 
        error:  main() cannot access f : X  is a private base class

WHY????????


#include <iostream.h>

class X{
        public:
        virtual void f();
};

class Y : virtual private X{


};

class Z : virtual private X{


};

class XYZ : public Y, public Z, virtual public X{
};


void X::f()
        {
        cout<<"\nX::f() ";
        }

int main(){
	XYZ * xyzp = new XYZ;
	xyzp->f();                 //LINE 31
}


Gary Randolph
randolph@ssd.kodak.com

jfw@rome.wpd.sgi.com (john fergus wilkinson) (11/22/90)

In article <1990Nov19.211251.13416@ssd.kodak.com>,
randolph@ssd.kodak.com (Gary L. Randolph) writes:
> 

> The following code results in the compile time error:
>    "tst.C", line 31: 
>         error:  main() cannot access f : X  is a private base class
> 
> WHY????????
> 
> 
> #include <iostream.h>
> 
> class X{
>         public:
>         virtual void f();
> };
> 
> class Y : virtual private X{
> 
> 
> };
> 
> class Z : virtual private X{
> 
> 
> };
> 
> class XYZ : public Y, public Z, virtual public X{
> };
> 
> 
> void X::f()
>         {
>         cout<<"\nX::f() ";
>         }
> 
> int main(){
> 	XYZ * xyzp = new XYZ;
> 	xyzp->f();                 //LINE 31
> }
> 
> 

According to the Annotated Reference Manual (Section 11.7), this is a
bug, and in fact it is fixed in cfront 2.1.