Team LiB
Previous Section Next Section

Chapter 14. Overloaded Operations and Conversions

第十四章 重载操作符与转换



Section 14.1 Defining an Overloaded Operator


Section 14.2 Input and Output Operators


Section 14.3 Arithmetic and Relational Operators


Section 14.4 Assignment Operators


Section 14.5 Subscript Operator


Section 14.6 Member Access Operators


Section 14.7 Increment and Decrement Operators


Section 14.8 Call Operator and Function Objects


Section 14.9 Conversions and Class Types


Chapter Summary


Defined Terms


In Chapter 5 we saw that C++ defines a large number of operators and automatic conversions among the built-in types. These facilities allow programmers to write a rich set of mixed-type expressions.

第五章介绍过,C++ 定义了许多内置类型间的操作符和自动转换。使用这些设施程序员能够编写丰富的混合类型表达式。

C++ lets us redefine the meaning of the operators when applied to objects of class type. It also lets us define conversion operations for class types. Class-type conversions are used like the built-in conversions to implicitly convert an object of one type to another type when needed.

C++ 允许我们重定义操作符用于类类型对象时的含义。如果需要,可以像内置转换那样使用类类型转换,将一个类型的对象隐式转换到另一类型。

Operator overloading allows the programmer to define versions of the operators for operands of class type. Chapter 13 covered the importance of the assignment operator and showed how to define the assignment operator. We first used overloaded operators in Chapter 1, when our programs used the shift operators (>> and <<) for input and output and the addition operator (+) to add two Sales_items. We'll finally see in this chapter how to define these overloaded operators.

通过操作符重载,程序员能够针对类类型的操作数定义不同的操作符版本。第十三章阐述了赋值操作符的重要性并介绍了怎样定义赋值操作符。我们第一次使用重载操作符是在第一章,那里程序用移位操作符(>><<)进行输入输出,用加号操作符(+)将两个 Sales_items 相加。本章我们终于可以看到怎样定义这些重载操作符了。

Through operator overloading, we can redefine most of the operators from Chapter 5 to work on objects of class type. Judicious use of operator overloading can make class types as intuitive to use as the built-in types. For example, the standard library defines several overloaded operators for the container classes. These classes define the subscript operator to access data elements and * and -> to dereference container iterators. The fact that these library types have the same operators makes using them similar to using built-in arrays and pointers. Allowing programs to use expressions rather than named functions can make the programs much easier to write and read. As an example, compare

通过操作符重载,可以重定义第五章介绍的大多数操作符,使它们用于类类型对象。明智地使用操作符重载可以使类类型的使用像内置类型一样直观。标准库为容器类定义了几个重载操作符。这些容器类定义了下标操作符以访问数据元素,定义了 *-> 对容器迭代器解引用。这些标准库的类型具有相同的操作符,使用它们就像使用内置数组和指针一样。允许程序使用表达式而不是命名函数,可以使编写和阅读程序容易得多。将

     cout << "The sum of " << v1 << " and " << v2
               << " is " << v1 + v2 << endl;

to the more verbose code that would be necessary if IO used named functions:

和以下更为冗长的代码相比较就能够看到。如果 IO 使用命名函数,类似下面的代码将无法避免:

     // hypothetical expression if IO used named functions
     cout.print("The sum of ").print(v1).
            print(" and ").print(v2).print(" is ").
            print(v1 + v2).print("\n").flush();

Team LiB
Previous Section Next Section