Team LiB
Previous Section Next Section

14.4. Assignment Operators

14.4. 赋值操作符

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.

第 13.2 节讨论了类类型对象对同类型其他对象的赋值。类赋值操作符接受类类型形参,通常,该形参是对类类型的 const 引用,但也可以是类类型或对类类型的非 const 引用。如果没有定义这个操作符,则编译器将合成它。类赋值操作符必须是类的成员,以便编译器可以知道是否需要合成一个。

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 定义了 3 个赋值操作符:除了接受 const string& 作为右操作数的类赋值操作符之外,类还定义了接受 C 风格字符串或 char 作为右操作数的赋值操作符,这些操作符可以这样使用:

     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

为了支持这些操作符,string 类包含如下成员:

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

赋值必须返回对 *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:

string 赋值操作符返回 string 引用,这与内置类型的赋值一致。而且,因为赋值返回一个引用,就不需要创建和撤销结果的临时副本。返回值通常是左操作数的引用,例如,这是 Sales_item 复合赋值操作符的定义:

     // 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.

定义一个赋值操作符,将 isbn 赋值给 Sales_item 对象。

Exercise 14.15:

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

第 14.2.1 节习题中介绍的 CheckoutRecord 类定义赋值操作符。

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.

CheckoutRecord 类还应该定义其他赋值操作符吗?如果是,解释哪些类型应该用作操作数并解释为什么。为这些类型实现赋值操作符。


Team LiB
Previous Section Next Section