Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS212: Object Oriented Analysis and Design Lecture 12: Operator Overloading-II.

Similar presentations


Presentation on theme: "CS212: Object Oriented Analysis and Design Lecture 12: Operator Overloading-II."— Presentation transcript:

1 CS212: Object Oriented Analysis and Design Lecture 12: Operator Overloading-II

2 Recap of Lecture 11 Operator overloading Unary operator Assignment operator Self assignment

3 Outline of lecture 12 Self assignment Use of Friend Function Type Conversion

4 Operator chaining Assignment is right-associative In order to support operator chaining, the assignment operator must return some value The value that should be returned is a reference to the left- hand side of the assignment. But should this reference be a const or nonconst? (a=b)=c; int a, b, c, d, e; a = b = c = d = e = 42;

5 Self assignment MUST CHECK FOR SELF-ASSIGNMENT !! Especially important when your class does its own memory allocation MyClass& MyClass::operator=(const MyClass &rhs) { // 1. Deallocate any memory that MyClass is using internally // 2. Allocate some memory to hold the contents of rhs // 3. Copy the values from rhs into this instance // 4. Return *this } MyClass mc;... mc = mc;

6 How to recover self assignment There are many ways to answer the question Are these two instances the same? Just compare the two objects' addresses MyClass& MyClass::operator=(const MyClass &rhs) { if (this != &rhs) {... // Deallocate, allocate new space, copy values... } return *this; }

7 Assignment operator The guidelines for the assignment operator are: Take a const-reference for the argument (the right-hand side of the assignment). Return a reference to the left-hand side, to support safe and reasonable operator chaining. (Do this by returning *this.) Check for self-assignment, by comparing the pointers (this to &rhs).

8 Using a Friend Function Overload an operator for a class by using a non-member function Usually a friend of the class Overloaded friend operator function is passed the operands explicitly Binary operator: two parameters, unary operator: one parameter. Demonstration

9 Flexibility with Friend Function Overload a binary operator by using a member function Object on the left side of the operator generates the call Further, a pointer to that object is passed in the this pointer. Ob + 100 // valid 100 + Ob // invalid Demonstration

10 New() and Delete() These operators can be overloaded globally or for a specific classes. Usually programmers do not overload them globally. void* operator new (size_t n) { //allocate memory and return the address } void operator delete (void *p) { //free the memory space pointer to by the pointer ‘p’ }

11 New() and delete() for Arrays // Allocate an array of objects. void *operator new[]( size_t size) { /* Perform allocation. Throw bad_alloc on failure. Constructor for each element called automatically. */ return pointer_to_memory; } // Delete an array of objects. void operator delete[] (void *p) { /* Free memory pointed to by p. Destructor for each element called automatically. */ }

12 Operator comma It is called when it appears next to an object of the type the comma is defined for. “operator,” is not called for function argument lists Only for objects that are out in the open, separated by commas. Demonstration

13 Some observations Return by value as const The return optimization return Integer(left.i + right.i); Integer tmp(left.i + right.i); return tmp; “make a temporary Integer object and return it.” Three things will happen.

14 Basic guidelines OperatorRecommended use All unary operatorsMember = ( ) [ ] –> and –>*must be member += –= /= *= ^= &= |= %= >>= <<= Member All other binary operators Non-member

15 Automatic type conversion If the compiler sees an expression or function call A type that isn’t quite the one it needs It can often perform an automatic type conversion From the type it has (source) to the type it wants (target) For user-defined types: automatic type conversion functions A particular type of constructor and an overloaded operator.

16 Constructor conversion A constructor that takes as its single argument an object (or reference) The type of object is different i.e. of another type Allows the compiler to perform an automatic type conversion Preventing constructor conversion: explicit

17 Operator conversion The second way is through operator overloading A member function that takes the current type and converts it to the desired type This form of operator overloading is unique You don’t appear to specify a return type The return type is the name of the operator you’re overloading. Demonstration

18 Pitfalls of type conversion Compiler must choose how to quietly perform a type conversion It can get into trouble if conversions are not designed correctly Ambiguity error when that conversion occurs Demonstration

19 Thank you Next Lecture: Inheritance


Download ppt "CS212: Object Oriented Analysis and Design Lecture 12: Operator Overloading-II."

Similar presentations


Ads by Google