[gnu.g++.bug] g++ 1.34.1 & private base classes

coleman@bert.dg.com (Kim Coleman) (03/29/89)

//  Test that compiler correctly handles private base classes. In particular,
//  an implicit conversion from a derived pointer to a base pointer should
//  only be made when the base class is public. See Stroustrap, p. 197.
//
//  Actually, g++ carries this rule even farther and performs implicit
//  conversions on objects and references as well as pointers. Though this
//  doesn't conform to The Book, if you're going to do this, it should
//  follow the same rules as pointers regarding private base classes.
//  As demonstrated below, this is only partially true.

class base {
    int i;
public:
    base()   {i = 0;}
    base (int x)  {i = x;}
    do_something (base& param) {;}
};

class derived : base {
    int j;
public:
     derived() {j = 0;}
};

main()
{
   base     base_obj;
   derived  derived_obj;

      //
      // The following statements should be in error on one hand because
      // you can only perform implicit pointer conversions between derived
      // and base class objects. However, even if you accept this behavior,
      // they should still be in error because <base> is private.
      //
   base_obj = derived_obj;                // g++ 1.34.1 only catches this
   base_obj.do_something (derived_obj);   // should also be an error
}

-------------------------------------
Kim Coleman
Data General Corp., Research Triangle Park, NC
{the world}!mcnc!rti!dg-rtp!coleman

tiemann@YAHI.STANFORD.EDU (Michael Tiemann) (05/03/89)

   class Object
   {
   public:
      virtual Object* some_method (const Object&);
   };
   
   
   class Collection_Member : public Object
   {};
   
   
   class Collection : public Object
   {
   public:
				    // this is what I WANT to do
      Collection_Member*  some_method (const Collection_Member&);
You can do this now (but this will be a *different* virtual function
then the one above.
   
   //
   // in desparation, I also tried:
   //
				    // g++ 1.34.1 accepts this form
No longer.
   //   Collection_Member*  some_method (const Object&);

Again, a *different* virtual function.
   //   Object*             some_method (const Collection_Member&);
   
				    // cfront only accepts this form
As does GNU C++.
   //   Object*             some_method (const Object&);
   };

Michael