Chapter 19.  Types and Expressions

Table of Contents

19.1. Operators
19.1.1. Table of Operators
19.2. Statements and Control Structures
19.2.1. Statements
19.2.2. Selection Statements
19.2.2.1. Exercises: Selection Statements
19.2.3. Iteration
19.2.3.1. Exercises: Iteration
19.2.4. Review Questions
19.3. Evaluation of Logical Expressions
19.4. Enumerations
19.5. Signed and Unsigned Integral Types
19.5.1. Exercises: Signed and Unsigned Integral Types
19.6. Standard Expression Conversions
19.6.1. Exercises: Standard Expression Conversions
19.7. Explicit Conversions
19.8. Safer ANSI C++ Typecast operators
19.8.1. static_cast and const_cast
19.8.1.1. Exercises: static_cast and const_cast
19.8.2. reinterpret_cast
19.8.3. Why Not Use C-Style Casts?
19.8.4. More About explicit Conversion Constructors
19.9. Overloading Special Operators
19.9.1. Conversion Operators
19.9.2. The Subscript operator[]
19.9.3. The Function Call operator()
19.9.3.1. Exercises: The Function Call operator()
19.10. Runtime Type Identification (RTTI)
19.10.1. typeid operator
19.11. Member Selection Operators
19.12. Exercises: Types and Expressions
19.13. Review Questions

[ fromfile: types.xml id: types ]

Abstract

This chapter seeks to provide a deeper understanding of C++'s strong typing system and shows how expressions are evaluated and converted.

Here we formally define some terms that we have been using. Operators are special kinds of functions that perform calculations on operands and return results. Operands are the arguments supplied to an operator.

Operators can be thought of as ordinary functions, except that you can call some of them using infix operator symbols (e.g., +, -, *, /, etc.). Thus, in addition to the longer function-call syntax (e.g., str3 = operator+(str1, str2); ) you can use the more readable infix systax (e.g., str3 = str1 + str2; ).

An expression can consist of a single operand, multiple operands with operators interspersed, or functions with arguments. Each expression has a type and a value. The value is obtained by applying the definitions of the operators (and/or functions) to the operands (and/or arguments).