Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 2304: Operator Overloading

Similar presentations


Presentation on theme: "CS 2304: Operator Overloading"— Presentation transcript:

1 CS 2304: Operator Overloading
Gusukuma 2015

2 Slides based on notes from Cornell and Caltech
html/Chapter12.html Gusukuma 2015

3 What is Operator Overloading?
Defines a function Operators at the end of the day are STILL functions Uses operators instead of parenthesis for function call Syntactic sugar Use to make code easier to write and READ Code is read more often than it is written Gusukuma 2015

4 Operator Overloading Syntax – Non-member functions
Unary operators (e.g. ++ and -- operators) returnType with the operator you’re overloading Replace arg with whatever your argument is returnType operator%(arg, int) This syntax for postfix operators Arg is the operand Binary Operators returnType right) right is the argument that is on right side of the operator The left side is itself Gusukuma 2015

5 Operator Overloading Syntax – Member functions
Unary Operator returnType with the operator you’re overloading The operand is itself returnType operator%(int) This syntax for postfix operators takes int for the argument The operator is itself Binary operator returnType with the binary operator you’re overloading right is the argument that is on right side of the operator The left side is itself Gusukuma 2015

6 Conventions – Assignment Operator
Signature MyClass& operator=(const MyClass& rhs); Note: Argument is const AND reference Note: return type is reference and NOT const Don’t want to change the right hand side, and you want the original, not a copy The caller of the assignment operator should return a reference to ITSELF (e.g. return *this) Gusukuma 2015

7 Assignment Operator Shenanigans
Assignment operator is right associative int a, b, c, d, e; a = b = c = d = e = 42; Interpret as: a = (b = (c = (d = (e = 42)))); (a = b) = c; MyClass mc; mc = mc; //Yea, self assignment Gusukuma 2015

8 Conventions – Assignment Operator
Steps to overloading the assignment operator Check for self assignment Deallocate dynamically allocated memory used by the object Allocate new memory (if necessary) for the object (if it uses dynamically allocated memory) Copy from right hand side Return self Gusukuma 2015

9 Conventions – Compound Assignment Operators
These are: +=, -=, *=, /= These are DESTRUCTIVE operators Tears down, possibly deallocates memory Steps for overloading Check for self assignment Do work (possibly reallocating memory) Return self Gusukuma 2015

10 Conventions – Binary Operators
Signature with operator) const MyClass MyClass& other); const type type& left, const type& right) Note: does NOT return a reference Note: returns const otherwise… (a + b) = c; Note: the return type and arguments can actually be all different, but that’s not conventional except for “==“ and “!=“, where the arguments are the same but return bool Gusukuma 2015

11 Binary Operator shortcut
const MyClass MyClass::operator+(const MyClass &other) const { MyClass result = *this; // Make a copy of myself using copy constructor result += other; // Use += to add other to the copy. return result; // All done! } Gusukuma 2015

12 Conventions – comparison operators
Returns bool “!=“ defined using “==“ For code reuse and consistency Arguments are const qualified Again, these operators shouldn’t modify the objects being compared Gusukuma 2015

13 Summary Binary operator overloads involve const arguments and const return type Assignment operator returns a reference, and takes in const arguments. Allocate and deallocate memory as appropriate Gusukuma 2015


Download ppt "CS 2304: Operator Overloading"

Similar presentations


Ads by Google