Operator Overloading Flashcards
Operator Overloading
What is operator overloading?
Defining certain operators as your own custom Functions / Methods
Operator Overloading
What are the two options for operator overloading?
A method and a function
Operator Overloading
What are the 3 rules of Operator Overloading?
- Must define all desired relationships. (int + class) != (class + int)
- extensible not mutable (i.e cant override existing overloads)
- Strict domain of valid operators
Operator Overloading
What are the characteristics of using a Function to overload and operator?
- It is free floating and not tied to a class.
- Must define both sides of the operator
Operator Overloading
What are the characteristics of using a Method to overload and operator?
- Built into the class itself
- Only define the right side of the operator
Operator Overloading
When would you use a Function to overload an operator?
When the left side of the operator is a primitive or pre-defined type.
Operator Overloading
When would you use a Method to overload and operator?
When the left side of the operator can be represented by the ‘This’ keyword.
Operator Overloading
What is the syntax for using a Function to overload an operator?
return_type operator (const type& lhs, const type& rhs) {
…
}
Operator Overloading
What is meant by operator overloading not being mutable?
- You cannot change an already defined relationship.
- You cannot add functionality between two primitives/fundamental types.
Operator Overloading
When overloading an operator, why are function arguments const?
Because an operator should not change its operands, but return a new object.
Operator Overloading
What are the main operators that cannot be overloaded?
- sizeof
- typeid
- scope resolution (::)
- ternary operators (? :)
- member acces operators (., .*)