Operator Overloading 1 Syntax The general syntax is

  • Slides: 9
Download presentation
Operator Overloading 1

Operator Overloading 1

Syntax The general syntax is: [friend] returntype operator<operator symbol> ( <parameters> ) { <statements>;

Syntax The general syntax is: [friend] returntype operator<operator symbol> ( <parameters> ) { <statements>; } 2

Syntax u The number of arguments in the overloaded operator’s argument list depends on

Syntax u The number of arguments in the overloaded operator’s argument list depends on two factors: u Whether it’s a unary operator (one argument) or a binary operator (two arguments). u Whether the operator is defined as a global function (one argument for unary, two for binary) or a member function (zero arguments for unary, one for binary – the object becomes the left-hand argument). 3

Operators that can’t be overloaded u All operators can be overloaded except for the

Operators that can’t be overloaded u All operators can be overloaded except for the ones below: u The member selection operator -. The pointer to member dereference operator. * Scope access/resolution operator - : : Conditional operator - ? : u u Also, there are no user-defined operators and you can’t change the precedence rules. 4

Overloaded operators as member or non-member functions? u u The decision is based on

Overloaded operators as member or non-member functions? u u The decision is based on what argument(s) is needed by the operator. In general, if it doesn’t make any difference, they should be members, to emphasize the association between the operator and its class. When the left-hand operand is always an object of the current class, this works fine. Sometimes you want the left-hand operand to be an object of some other class, then the overloaded operator cannot be a member function. 5

Some ”tricky” operators u Increment and decrement operators u ++a (a pre-increment) generates a

Some ”tricky” operators u Increment and decrement operators u ++a (a pre-increment) generates a call to operator++(a); a++ (a post-increment) generates a call to operator++(a, int) u u u for non-member functions. The member function versions, would be: B: : operator++( ); and B: : operator++(int); Note: a dummy value is passed by the compiler to distinguish (generate different signatures) the two versions. 6

Some ”tricky” operators u The assignment operator Defining the assignment operator has a lot

Some ”tricky” operators u The assignment operator Defining the assignment operator has a lot in common with defining the copy constructor and the destructor. List& List: : operator=( const List& b ) { if( this != &b) //check for self assignment { free(); copy(b); } return *this; } u 7

An example u Let’s look at our example implementation of the class ’Values’ 8

An example u Let’s look at our example implementation of the class ’Values’ 8

Arguments and return values u Guidelines u Return by value as const u The

Arguments and return values u Guidelines u Return by value as const u The return value optimization 9