Ch 7. Operator Overloading


Click here to start

Table of Contents

Ch 7. Operator Overloading


Figure 7.1 Overloadable Operators in C++

Overloaded Functions or Methods

Figure 7.2 Comparison Defined as an Ordinary Function

Figure 7.3 Comparison Defined as a Member Function

Simple Binary Operators

Simple Binary Operators

The Comparison Operators

The Increment and Decrement Operators

class box { public: box (int v) : value(v) { }

class box { public: ... const box & operator ++ () { value++; return *this; }

Avoid expressions whose meanings are not completely clear.

The Shift Operators

ostream & operator <<

The Assignment Operator

Always redefine the assignment operator in classes that include a pointer value.

Always check for self-assignment.

Despite the use of the assignment symbol, constructors do not use the assignment operator.

class bigbox : public box { public: bigbox (int i, double d) : box(i), dvalue(d) { } void operator = (bigbox & right) { value = right.value; dvalue = right.dvalue; } protected: double dvalue;

The Compound Assignment Operators

The Subscript Operator

The real vector data type does not check subscript ranges.

The Parenthesis Operator

LargerThan tester(12);

The Address-of Operator

The Logical Connectives

The Comma Operator

Mistakenly taping a comma instead of a period can be a very subtle programming error.

An overloaded comma operator can never have the same short-circuit semantics as the original.

The Arrow Operator

Example of Arrow Operator

Conversion Operators

Example of Conversion Operators

Memory management Operators

Disallowing Operators

Implicit Functions and Invocations

Implicitly Created Operations

PPT Slide

Implicit Constructors

Implicit Destructors

Implicit Assignment Operator

Implicit Address and Comma

Implicit Function Invocations

class box { public: box () { value = 0; } box (int i) { value = i; } box (box & a) { value = a.value; } ~box() { } // destructor void operator = (box & right) { value = right.value;} operator int () { return value; } private: int value;

PPT Slide

PPT Slide

PPT Slide

PPT Slide

Author: Seung Sean Yoo


Home Page: