Section 14.4. Assignment Operators


14.4. Assignment Operators

We covered the assignment of one object of class type to another object of its type in Section 13.2 (p. 482). The class assignment operator takes a parameter that is the class type. Usually the parameter is a const reference to the class type. However, the parameter could be the class type or a nonconst reference to the class type. This operator will be synthesized by the compiler if we do not define it ourselves. The class assignment operator must be a member of the class so the compiler can know whether it needs to synthesize one.

Additional assignment operators that differ by the type of the right-hand operand can be defined for a class type. For example, the library string class defines three assignment operators: In addition to the class assignment operator, which takes a const string& as its right-hand operand, the string class defines versions of assignment that take a C-style character string or a char as the right-hand operand. These might be used as follows:

      string car ("Volks");      car = "Studebaker"; // string = const char*      string model;      model = 'T'; // string = char 

To support these operations, the string class contains members that look like

      // illustration of assignment operators for class string      class string {      public:          string& operator=(const string &);      // s1 = s2;          string& operator=(const char *);        // s1 = "str";          string& operator=(char);                // s1 = 'c';          // ....       }; 

Assignment operators can be overloaded. Unlike the compound-assignment operators, every assignment operator, regardless of parameter type, must be defined as a member function.



Assignment Should Return a Reference to *this

The string assignment operators return a reference to string, which is consistent with assignment for the built-in types. Moreover, because assignment returns a reference there is no need to create and destroy a temporary copy of the result. The return value is usually a reference to the left-hand operand. For example, here is the definition of the Sales_item compound-assignment operator:

      // assumes that both objects refer to the same isbn      Sales_item& Sales_item::operator+=(const Sales_item& rhs)      {         units_sold += rhs.units_sold;         revenue += rhs.revenue;         return *this;      } 

Ordinarily, assignment operators and compound-assignment operators ought to return a reference to the left-hand operand.



Exercises Section 14.4

Exercise 14.14:

Define a version of the assignment operator that can assign an isbn to a Sales_item.

Exercise 14.15:

Define the class assignment operator for the CheckoutRecord introduced in the exercises to Section 14.2.1 (p. 515).

Exercise 14.16:

Should CheckoutRecord define any other assignment operators? If so, explain which types should be used as operands and why. Implement the assignment operators for those types.




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