Section 13.2. The Assignment Operator


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:

      Sales_item trans, accum;      trans = accum; 

As with the copy constructor, the compiler synthesizes an assignment operator if the class does not define its own.

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 member) as the operator has operands. Assignment is binary, so the operator function has two parameters: The first parameter corresponds to the left-hand operand, and the second to the right-hand operand.

Most operators may be defined as member or nonmember functions. When an operator is a member function, its first operand is implicitly bound to the this pointer. Some operators, assignment among them, must be members of the class for which the operator is defined. Because assignment must be a member of its class, this is bound to a pointer to the left-hand operand. The assignment operator, therefore, takes a single parameter that is an object of the same class type. Usually, the right-hand operand is passed as a const reference.

The return type from the assignment operator should be the same as the return from assignment for the built-in types (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:

      // 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;      } 

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.

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 either the copy constructor or the assignment operator: The synthesized versions of these operators work fine.

However, a class may define its own assignment operator. In general, if a class needs a copy constructor, it will also need an assignment operator.

In fact, these operations should be thought of as a unit. If we require one, we almost surely require the other.



We'll see examples of classes that need to define their own assignment operators in Section 13.4 (p. 486) and Section 13.5 (p. 492).

Exercises Section 13.2

Exercise 13.7:

When does a class need to define an assignment operator?

Exercise 13.8:

For each type listed in the first exercise in Section 13.1.2 (p. 481) indicate whether the class would need an assignment operator.

Exercise 13.9:

The first exercise in 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.




C++ Primer
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2006
Pages: 223
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net