[comp.lang.c++] Operator Inheritance

vaughan@puma.cad.mcc.com (Paul Vaughan) (08/29/89)

The following code was compiled with g++ 1.35 and also with the test
version of 1.36.  Both produced the error message shown.  I need to
know if this is correct behavior or not.  Also, I need to know if
virtual operators should work in the same manner as virtual functions.

class A { public:
  operator=(int i) {value=i;}
private:
  int value;
};

class B : public A {
public:
};

main() {
  A a;
  a = 3;
  B b;
  b.operator=(3);
  b = 3; //vop.cc:17: assignment not defined for type `B'
}

Being able to use the functional syntax but not the operator syntax seems inconsistent.

 Paul Vaughan, MCC CAD Program | ARPA: vaughan@mcc.com | Phone: [512] 338-3639
 Box 200195, Austin, TX 78720  | UUCP: ...!cs.utexas.edu!milano!cadillac!vaughan

randyh@uswat.uswest.com ( Randy Hughes #3000 x2116 ) (09/14/90)

I am currently writing a library that will be used by applications
through inheritance. I would like the base classes in the library
to provide operators that are accessible by the derived class objects
in the application.

Consider the following simple example:


class base	// in the library
{
 public:
  int x;
  base &operator=(int v) { x = v; return *this; }
  base &operator+=(int v) { x += v; return *this; }
  base operator+(int v) { base x = *this; x += v; return *this; }
};

class derived: public base	// in the application
{
};

main()
{
  derived d;

  d = 1;	// bombs on compilation (cannot make a derived)
  d += 1;	// compiles
  d = d;	// compiles
  d = (d += 1);	// bombs on compilation (cannot make a derived)
  d + 1;	// compiles (gives warning about result not used)
  d = d + 1;	// bombs on compilation (cannot make a derived)
}



My questions are:

1) Are the failures due to base objects not being able to be assigned
   to derived objects?

2) Changing the derived class to:

class derived: public base	// in the application
{
 public:
  derived() { }
  derived(const base &b):base(b) { }
};

   to allow the conversion from base to derived gives compilation errors
   seeming to indicate that the operator= function from the base class is
   indeed not inherited. Is operator= handled specially in inheritance?

3) Is there any way to allow the derived class to use all the base class
   operators without requiring additions to the derived class (my guess is
   no)?

4) What is the least amount of work required for the derived class to be
   able to use all of the base class operators? I don't want to put much
   burden on the user of the library base class. Also, the number of base
   class operators may be extensive.


-------------------------------------------
Randy Hughes
U S WEST Advanced Technologies