Topic: Default operator=


Author: jason@cygnus.com (Jason Merrill)
Date: Sun, 12 Jun 1994 03:49:29 GMT
Raw View
In the code below, which of the numbered lines are copy assignment
operators?  In other words, which of those lines will prevent the default
operator= from being generated for B?  Cfront says 2-4.  xlC says 2.  By my
reading, the WP says none:

8   If a class  X has any  X::operator=() that has a parameter of class
    X, the default assignment will not be generated.

On the other hand, I think that allowing #2 to be a copy assignment
operator is appropriate, so that copy assignment can be made virtual
without having to write a stub function like the second member of C here

struct C: public B {
  virtual C& operator = (const A& a) { ... };
  C& operator = (const C& c) { return operator= ((const A&)c); }
};

for each derived class.  Perhaps it should be constrained so that it is
only allowed if the base type used as the parameter (e.g. `A') defines its
copy assignment operator to be virtual.

Jason

struct A { };

struct X {
  X (const A&) { }
};

struct B: public A {
  operator int () { return 1; }
  // B& operator= (const A&) { return *this; }        // #1
  // virtual B& operator= (const A&) { return *this; } // #2
  // virtual B& operator= (int) { return *this; }      // #3
  // virtual B& operator= (const X&) { return *this; } // #4
};

Jason




Author: b91926@fnclub.fnal.gov (David Sachs)
Date: 11 Nov 1993 20:31:15 GMT
Raw View
What is the "proper" behavior for a defaultly generated operator= for a class with bases, especially virtual bases? Should operator= call the operator= for its base classes?

e.g. What is the proper output of the following program? I do not feel that operator= for the virtual base class a should be called twice.

#include <iostream.h>
class a
{
  private:
  int a1;
  protected:
  const a& operator=(const a& z);
  a() {a1=0;}
  a(const a&) {}
};

const a& a::operator=(const a& z)
{
  a1 = z.a1;
  cout << "testing" << endl;
  return *this;
}

class b1: protected virtual a
{
  private:
  int bx;
};

class b2: protected virtual a
{
  private:
  int by;
};

class b: b1, b2
{
  public:
  int bz;
};

b x1;
b x2;

int main(void)
{
  x1 = x2;
  return 0;
}