Overloaded Assignment

You can only overload an assignment operator by declaring a nonstatic member function. The following example shows how you can overload the assignment operator for a particular class:

class X
{
public:
      X();
      X& operator=(X&);
      X& operator=(int);
};
X& X::operator=(X& x) { /* ... */ }
X& X::operator=(int i) { /* ... */ }

void main()
{
      X x1, x2;
      x1 = x2;      // call x1.operator=(x2)
      x1 = 5;       // call x1.operator=(5)
}

You cannot declare an overloaded assignment operator that is a nonmember function. Overloaded assignment operators are not inherited.

If a copy assignment operator function is not defined for a class, the copy assignment operator function is defined by default as a memberwise assignment of the class members. If assignment operator functions exist for base classes or class members, these operators are used when the compiler generates default copy assignment operators.