Operator Overloading
Examples:
Same operation, different data types:
+ for addition for both integers, real numbers
Different operations, same type:
- for subtraction (binary), for negation (unary)
Different operations, different types:
* for multiplication (binary), for dereferencing pointers in C++ (unary)
Advantages:
No need to invent numerous operator symbols. E.g., type-specific addition
Intuitive (We do not use data types in real life)
Disadvantages:
Readability Impaired
& symbol used for both bitwise AND, Address operator in C++
Errors Harder to Detect
doublevar = intvar1 / intvar2;
Writability?
Case Study:
Operator overloading in C++