cs212: object oriented analysis and design lecture 11: operator overloading-i
Post on 18-Jan-2018
219 Views
Preview:
DESCRIPTION
TRANSCRIPT
Recap of Lecture 10
• Function Overloading
• Name Mangling
• Resolution of name and type
• Copy constructor
Outline of lecture 11
• Operator overloading
• Unary operator
• Assignment operator
• Self assignment
PolymorphismPolymorphism in C++
Compile Time
Function overloading
Operator overloadin
g
Runtime
Virtual Function
Operator overloading
Compiler sees an expression consisting of an argument followed by an operator followed by an argument, it simply calls a function.
An operator is simply a function call with a different syntax.
There must be a previously declared function to match that operator
Operator overloading is just “syntactic sugar”
Is the knowledge about operator wrong ?• Maybe everything they know about operators in C is suddenly
wrong
• Operators for built-in types won’t suddenly start working differently
• Overloaded operators can be created only where new data types are involved
1 << 4;
1.414 << 1;
won’t suddenly change its meaning
won’t suddenly start working.
Operator overloading
• Specify more than one definition for an operator in the same scope
• Declared (or defined) previously in the same scope
• The compiler determines the most appropriate definition to use
• Overload resolution
• Redefine or overload most of the built-in operators available
We are already overloading
• Inserters (<<) and extractors (>>)
• A stream is an object that formats and holds bytes.
int i;cin >> i;
float f;cin >> f;
char c;cin >> c;
char buf[100];cin >> buf;
Operator overloading
• Overloaded operators are functions with special names
• The overloaded function will perform relative to the class upon which it will work
• The keyword operator followed by the symbol for the operator being defined
• Operator functions can be member or non-member of a class
• Non-member operator function : friend functions
Creating a Member operator function• The general form of such a function is
Ret-type classname::operator#(arg-list){//operations
}
// Overload + for loc.loc loc::operator+(loc op2)
Demonstration
Can we overload all operators+ - * / % ^
& | ~ ! , =
< > <= >= ++ --
<< >> == != && ||
+= -= /= %= ^= &=
|= *= <<= >>= [] ()
-> ->* new new [] delete delete []
:: .* . ?:
Parameter passing to operator function
Ope
rato
r fun
ctio
n
GlobalUnary (1)
Binary (2)
MemberUnary (0)
Binary (1)
The number of arguments in the overloaded operator’s argument list depends on two factors
Restrictions
• Cannot combine operators that currently have no meaning
• Cannot change the evaluation precedence of operators
• Cannot change the number of arguments required by an operator
Overloading Unary Operator
• Takes just one operand
• Takes no argument
• Example:• Increment (++) and decrement(--) operator• Unary minus (-) operator• The logical not (!) operator
• The unary operators operate on the object for which they were called
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?
int a, b, c, d, e;
a = b = c = d = e = 42;
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;
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;
}
Assignment operatorThe 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).
top related