Team LiB
Previous Section Next Section

Chapter 5. Expressions

第五章 表达式

CONTENTS

Section 5.1 Arithmetic Operators

149

Section 5.2 Relational and Logical Operators

152

Section 5.3 The Bitwise Operators

154

Section 5.4 Assignment Operators

159

Section 5.5 Increment and Decrement Operators

162

Section 5.6 The Arrow Operator

164

Section 5.7 The Conditional Operator

165

Section 5.8 The sizeof Operator

167

Section 5.9 Comma Operator

168

Section 5.10 Evaluating Compound Expressions

168

Section 5.11 The new and delete Expressions

174

Section 5.12 Type Conversions

178

Chapter Summary

188

Defined Terms

188


C++ provides a rich set of operators and defines what these operators do when applied to operands of built-in type. It also allows us to define meanings for the operators when applied to class types. This facility, known as operator overloading, is used by the library to define the operators that apply to the library types.

C++ 提供了丰富的操作符,并定义操作数为内置类型时,这些操作符的含义。除此之外,C++ 还支持操作符重载,允许程序员自定义用于类类型时操作符的含义。标准库正是使用这种功能定义用于库类型的操作符。

In this chapter our focus is on the operators as defined in the language and applied to operands of built-in type. We will also look at some of the operators defined by the library. Chapter 14 shows how we can define our own overloaded operators.

本章重点介绍 C++ 语言定义的操作符,它们使用内置类型的操作数;本章还会介绍一些标准库定义的操作符。第十四章将学习如何定义自己的重载操作符。

An expression is composed of one or more operands that are combined by operators. The simplest form of an expression consists of a single literal constant or variable. More complicated expressions are formed from an operator and one or more operands.

表达式由一个或多个操作数通过操作符组合而成。最简单的表达式仅包含一个字面值常量或变量。较复杂的表达式则由操作符以及一个或多个操作数构成。

Every expression yields a result. In the case of an expression with no operator, the result is the operand itself, e.g., a literal constant or a variable. When an object is used in a context that requires a value, then the object is evaluated by fetching the object's value. For example, assuming ival is an int object,

每个表达式都会产生一个结果。如果表达式中没有操作符,则其结果就是操作数本身(例如,字面值常量或变量)的值。当一个对象用在需要使用其值的地方,则计算该对象的值。例如,假设ival是一个int型对象:

     if (ival)            // evaluate ival as a condition
         // ....

we could use ival as an expression in the condition of an if. The condition succeeds if the value of ival is not zero and fails otherwise.

上述语句将 ival 作为 if 语句的条件表达式。当 ival 为非零值时, if 条件成立;否则条件不成立。

The result of expressions that involve operators is determined by applying each operator to its operand(s). Except when noted otherwise, the result of an expression is an rvalue (Section 2.3.1, p. 45). We can read the result but cannot assign to it.

对于含有操作符的表达式,它的值通过对操作数做指定操作获得。除了特殊用法外,表达式的结果是右值(第 2.3.1 节),可以读取该结果值,但是不允许对它进行赋值。

The meaning of an operatorwhat operation is performed and the type of the resultdepends on the types of its operands.

操作符的含义——该操作符执行什么操作以及操作结果的类型——取决于操作数的类型。



Until one knows the type of the operand(s), it is not possible to know what a particular expression means. The expression

除非已知道操作数的类型,否则无法确定一个特定表达式的含义。下面的表达式

     i + j

might mean integer addition, concatenation of strings, floating-point addition, or something else entirely. How the expression is evaluated depends on the types of i and j.

既可能是整数的加法操作、字符串的串接或者浮点数的加法操作,也完全可能是其他的操作。如何计算该表达式的值,完全取决于 ij 的数据类型。

There are both unary operators and binary operators. Unary operators, such as address-of (&) and dereference (*), act on one operand. Binary operators, such as addition (+) and subtraction (-), act on two operands. There is also one ternary operator that takes three operands. We'll look at this operator in Section 5.7 (p. 165).

C++提供了一元操作符二元操作符两种操作符。作用在一个操作数上的操作符称为一元操作符,如取地址操作符(&)和解引用操作符(*);而二元操作符则作用于两个操作数上,如加法操作符(+)和减法操作符(-)。除此之外,C++ 还提供了一个使用三个操作数的三元操作符(ternary operator),我们将在第 5.7 节介绍它。

Some symbols, such as *, are used to represent both a unary and a binary operator. The * symbol is used as the (unary) dereference operator and as the (binary) multiplication operator. The uses of the symbol are independent; it can be helpful to think of them as two different symbols. The context in which an operator symbol is used always determines whether the symbol represents a unary or binary operator.

有些符号(symbols)既可表示一元操作也可表示二元操作。例如,符号 * 既可以作为(一元)解引用操作符,也可以作为(二元)乘法操作符,这两种用法相互独立、各不相关,如果将其视为两个不同的符号可能会更容易理解些。对于这类操作符,需要根据该符号所处的上下文来确定它代表一元操作还是二元操作。

Operators impose requirements on the type(s) of their operand(s). The language defines the type requirements for the operators when applied to built-in or compound types. For example, the dereference operator, when applied to an object of built-in type, requires that its operand be a pointer type. Attempting to dereference an object of any other built-in or compound type is an error.

操作符对其操作数的类型有要求,如果操作符应用于内置或复合类型的操作数,则由C++语言定义其类型要求。例如,用于内置类型对象的解引用操作符要求其操作数必须是指针类型,对任何其他内置类型或复合类型对象进行解引用将导致错误的产生。

The binary operators, when applied to operands of built-in or compound type, usually require that the operands be the same type, or types that can be converted to a common type. We'll look at conversions in Section 5.12 (p. 178). Although the rules can be complex, for the most part conversions happen in expected ways. For example, we can convert an integer to floating-point, and vice versa, but we cannot convert a pointer type to floating-point.

对于操作数为内置或复合类型的二元操作符,通常要求它的两个操作数具有相同的数据类型,或者其类型可以转换为同一种数据类型。关于类型转换,我们将在第 5.12 节学习。尽管规则可能比较复杂,但大部分的类型转换都可按预期的方式进行。例如,整型可转换为浮点类型,反之亦然,但不能将指针类型转换为浮点类型。

Understanding expressions with multiple operators requires understanding operator precedence, associativity, and the order of evaluation of the operands. For example, the expression

要理解由多个操作符组成的表达式,必须先理解操作符的优先级结合性和操作数的求值顺序。例如,表达式

     5 + 10 * 20/2;

uses addition, multiplication, and division. The result of this expression depends on how the operands are grouped to the operators. For example, the operands to the * operator could be 10 and 20, or 10 and 20/2, or 15 and 20 or 15 and 20/2. Associativity and precedence rules specify the grouping of operators and their operands. In C++ this expression evaluates to 105, which is the result of multiplying 10 and 20, dividing that result by 2, and then adding 5.

使用了加法、乘法和除法操作。该表达式的值取决于操作数与操作符如何结合。例如,乘法操作符 * 的操作数可以是 1020,也可以是 1020 /2,或者 15201520/2。结合性和优先级规则规定了操作数与操作符的结合方式。在 C++ 语言中,该表达式的值应是 1051020 先做乘法操作,然后其结果除以 2,再加 5 即为最后结果。

Knowing how operands and operators are grouped is not always sufficient to determine the result. It may also be necessary to know in what order the operands to each operator are evaluated. Each operator controls what assumptions, if any, can be made as to the order in which the operands will be evaluatedthat is, whether we can assume that the left-hand operand is always evaluated before the right or not. Most operators do not guarantee a particular order of evaluation. We will cover these topics in Section 5.10 (p. 168).

求解表达式时,仅了解操作数和操作符如何结合是不足够的,还必须清楚操作符上每一个操作数的求值顺序。每个操作符都控制了其假定的求值顺序,即,我们是否可以假定左操作数总是先于右操作数求值。大部分的操作符无法保证某种特定的求值次序,我们将于第 5.10 节讨论这个问题。

Team LiB
Previous Section Next Section