13.2. The Assignment Operator
Just as classes control how objects are initialized, they also define what happens when objects of their type are assigned:
As with the copy constructor, the compiler synthesizes an assignment operator if the class does not define its own.
Sales_item trans, accum;
trans = accum;
Introducing Overloaded Assignment
Before we look at the synthesized assignment operator, we need to know a bit about overloaded operators, which we cover in detail in Chapter 14.Overloaded operators are functions that have the name operator followed by the symbol for the operator being defined. Hence, we define assignment by defining a function named operator=. Like any other function, an operator function has a return type and a parameter list. The parameter list must have the same number of parameters (including the implicit this parameter if the operator is a Section 5.4.1, p. 160). Assignment to a built-in type returns a reference to its left-hand operand. Therefore, the assignment operator also returns a reference to the same type as its class.For example, the assignment operator for Sales_item might be declared as
class Sales_item {
public:
// other members as before
// equivalent to the synthesized assignment operator
Sales_item& operator=(const Sales_item &);
};
The Synthesized Assignment Operator
The synthesized assignment operator operates similarly to the synthesized copy constructor. It performs memberwise assignment: Each member of the right-hand object is assigned to the corresponding member of the left-hand object. Except for arrays, each member is assigned in the usual way for its type. For arrays, each array element is assigned.As an example, the synthesized Sales_item assignment operator would look something like:
The synthesized assignment operator assigns each member in turn, using the built-in or class-defined assignment operator as appropriate to the type of the member. The operator returns *this, which is a reference to the left-hand object.
// equivalent to the synthesized assignment operator
Sales_item&
Sales_item::operator=(const Sales_item &rhs)
{
isbn = rhs.isbn; // calls string::operator=
units_sold = rhs.units_sold; // uses built-in int assignment
revenue = rhs.revenue; // uses built-in double assignment
return *this;
}
Copy and Assign Usually Go Together
Classes that can use the synthesized copy constructor usually can use the synthesized assignment operator as well. Our Sales_item class has no need to define Section 13.4 (p. 486) and Section 13.5 (p. 492).
Exercises Section 13.2
Section 13.1.2 (p. 481) indicate whether the class would need an assignment operator.Section 13.1.2 (p. 481) included a skeletal definition for class NoName. Determine whether that class needs an assignment operator. If so, implement it.Exercise 13.10:Define an Employee class that contains the employee's name and a unique employee identifier. Give the class a default constructor and a constructor that takes a string representing the employee's name. If the class needs a copy constructor or assignment operator, implement those functions as well.