14.3. Arithmetic and Relational Operators14.3. 算术操作符和关系操作符Ordinarily, we define the arithmetic and relational operators as nonmember functions, as we do here with our Sales_item addition operator: 一般而言,将算术和关系操作符定义为非成员函数,像下面给出的 Sales_item 加法操作符一样: // assumes that both objects refer to the same isbn Sales_item operator+(const Sales_item& lhs, const Sales_item& rhs) { Sales_item ret(lhs); // copy lhs into a local object that we'll return ret += rhs; // add in the contents of rhs return ret; // return ret by value } The addition operator doesn't change the state of either operand; the operands are references to const objects. Instead, it generates and returns a new Sales_item object, which is initialized as a copy of lhs. We use the Sales_item compound-assignment operator to add in the value of rhs. 加法操作符并不改变操作符的状态,操作符是对 const 对象的引用;相反,它产生并返回一个新的 Sales_item 对象,该对象初始化为 lhs 的副本。我们使用 Sales_item 的复合赋值操作符来加入 rhs 的值。
An arithmetic operator usually generates a new value that is the result of a computation on its two operands. That value is distinct from either operand and is calculated in a local variable. It would be a run-time error to return a reference to that variable. 算术操作符通常产生一个新值,该值是两个操作数的计算结果,它不同于任一操作数且在一个局部变量中计算,返回对那个变量的引用是一个运行时错误。
It is simpler and more efficient to implement the arithmetic operator (e.g., +) in terms of the compound-assignment operator (e.g., +=) rather than the other way around. As an example, consider our Sales_item operators. If we implemented += by calling +, then += would needlessly create and destroy a temporary to hold the result from +. 根据复合赋值操作符(如 +=)来实现算术操作符(如 +),比其他方式更简单且更有效。例如,我们的 Sales_item 操作符。如果我们调用 += 来实现 +,则可以不必创建和撤销一个临时量来保存 + 的结果。 14.3.1. Equality Operators14.3.1. 相等操作符Ordinarily, classes in C++ use the equality operator to mean that the objects are equivalent. That is, they usually compare every data member and treat two objects as equal if and only if all corresponding members are the same. In line with this design philosophy, our Sales_item equality operator should compare the isbn as well as the sales figures: 通常,C++ 中的类使用相等操作符表示对象是等价的。即,它们通常比较每个数据成员,如果所有对应成员都相同,则认为两个对象相等。与这一设计原则一致,Sales_item 的相等操作符应比较 isbn 以及销售数据: inline bool operator==(const Sales_item &lhs, const Sales_item &rhs) { // must be made a friend of Sales_item return lhs.units_sold == rhs.units_sold && lhs.revenue == rhs.revenue && lhs.same_isbn(rhs); } inline bool operator!=(const Sales_item &lhs, const Sales_item &rhs) { return !(lhs == rhs); // != defined in terms of operator== } The definition of these functions is trivial. More important are the design principles that these functions embody: 这些函数的定义并不重要,重要的是这些函数所包含的设计原则:
14.3.2. Relational Operators14.3.2. 关系操作符Classes for which the equality operator is defined also often have relational operators. In particular, because the associative containers and some of the algorithms use the less-than operator, it can be quite useful to define an operator<. 定义了相等操作符的类一般也具有关系操作符。尤其是,因为关联容器和某些算法使用小于操作符,所以定义 operator< 可能相当有用。 Although we might think our Sales_item class should support the relational operators, it turns out that it probably should not. The reasons are somewhat subtle and deserve understanding. 我们也许认为 Sales_item 类应该支持关系操作符,但恰恰相反,它很可能不应该支持关系操作符,原因有些微妙,值得了解。 As we'll see in Chapter 15, we might want to use an associative container to hold Sales_item transactions. When we put objects into the container, we'd want them ordered by ISBN, and wouldn't care whether the sales data in two records were different. 正如第十五章将要介绍的,我们可能想要使用关联容器来保存 Sales_item 事务。将对象放在容器中时,我们希望它们按 ISBN 排序,而不会关心两个记录中的销售数据是否不同。 However, if we were to define operator< as comparison on isbn, that definition would be incompatible with the obvious definition of ==. If we had two transactions for the same ISBN, neither record would be less than the other. Yet, if the sales figures in those objects were different, then these objects would be !=. Ordinarily, if we have two objects, neither of which is less than the other, then we expect that those objects are equal. 但是,如果将 operator< 定义为对 isbn 的比较,该定义将与前面 == 的定义不相容。如果有两个针对同一 ISBN 的事务,其中任意一个都不会小于另一个,然而,如果这两个对象中的销售数据不同,则它们就不相等。但是,一般说来,如果有两个对象,其中任意一个都不小于另一个,则认为它们相等。 Because the logical definition of < is inconsistent with the logical definition of ==, it is better not to define < at all. We'll see in Chapter 15 how to use a separate named function to compare Sales_items when we want to store them in an associative container. 因为 < 的逻辑定义与 == 的逻辑定义不一致,所以根本不定义 < 会更好。第十五章将会介绍想要将 Sales_items 对象存储到关联容器中时,怎样使用单独的命名函数来比较 Sales_items 对象。
|