If you do not define an assignment operator and one is required, a default assignment operator is defined. If you do not define an assignment operator and one is not required, a default assignment operator is declared but not defined. If an assignment operator that takes a single argument of a class type exists for a class, a default assignment operator is not generated.
Copy by assignment is used only in assignment.
You can define an assignment operator for a class with a single argument that is a constant reference to that class type, only if all its base classes and members have assignment operators that accept constant arguments .
For example:
class B1
{
public:
B1& operator=(const B1&);
};
class D: public B1
{
public:
D& operator=(const D&);
};
D& D::operator=(const D& dobj) {D dobj2 = dobj;
return dobj2;}
Otherwise, you can define an assignment operator for a class
with a single argument that is a reference to that class type.
For example:
class Z
{
public:
Z& operator=( Z&);
};
Z& Z::operator=(Z& zobj) {Z zobj2 = zobj;
return zobj2;}
The default assignment operator for a class is a public class member. The return type is a reference to the class type it is a member of.
![]()
Overloaded Assignment
Copy by Initialization