Lecture 7 - Operator Overloading
Does adding const to a method in the derived class (same return type, function name, and parameters) override the method?
DOESN'T override, but hides methods of same name
Nonmember operator function:
LHS: MAY be object of operators class, different class, or built-in type
Member operator function:
Left operand: must be class object or reference to class object If need private or protected data from RHS, function must be either - friend - use accessor or mutator methods
Which two operators does the compiler generate a default implementation if none is supplied?
- assignment operator (=) - member-wise assignment of class' data members - address operator (&) - returns address of object in memory (DO NOT OVERRIDE THIS)
Which operators cannot be overloaded? (give 5 examples)
. .* :: ?: sizeof
An overloaded operator must have at least one operand that is (1). This prevents what?
1) a user defined type 2) overloading operators for standard data types
To call the const operator[] from the non-const operator[] what steps do you need to take?
1) cast away const of op[] return type 2) add const to *this type
Non-member functions are made (1) if they need access to the internal state.
1) friends
Operator overloading is invoked using (1) syntax.
1) infix
Friendship is (1), but not (2).
1) inherited 2) commutative
Overloading an operator doesn't change what? (4)
1) operator precedence 2) associativity of operator (left vs right) 3) arity of operator (unary/binary) 4) meaning of operator for built-in types
The a++ operator injects a (1) that isn't in the ++a operator.
1) phantom parameter (int)
Is prefix or postfix notation preferred for incrementing? Why?
1) prefix 2) postfix calls prefix signature --> 2 copies created
In overriding, the (1) alone can't vary.
1) return type
operator[] for reading a value is (1) usage. operator[] for writing a value is (2) usage.
1) rvalue (value) 2) lvalue (reference)
Overloaded operator must adhere to the (1) of the original operator.
1) syntax
Operator overloading:
allows programmer to supply behavior that occurs when built-in operators are applied to user-defined types
Which of the Big 3 is an example of operator overloading?
assignment operator
Operator overloading can't be used for...
creating new operator symbols
Hiding:
derived class hides methods in the base if it has the same NAME as a method in the base class
Infix vs Prefix syntax:
infix: 1 + 2 + 3 prefix: (+ (+ 1 2) 3)
Preference for overloading unary operators?
make them class members
Operator functions are defined as what?
member functions or non-member functions
Overloading:
multiple functions with the same name occur in the same scope, but with different signatures
Unary operator:
operator doesn't take a parameter (involves one object, this)
Binary operator:
operator takes a parameter (involves two objects, this and param)
Each individual operator must be overloaded for use with user defined types (example?)
overloading operator= and operator- DOESN'T overload operator-=
Operator invocation syntax: (programmer preferred and computer preferred)
programmer: a = b computer: a.operator=(b)
How do you explicitly disable hiding?
under public: "using baseClass:methodName;"
Overriding:
when a derived class function has the same name AND signature as a base class virtual function (involves inheritance)
How do you write an operator overload?
write function definition same as function name of operator, followed by symbol for the operator to be overloaded: operator-
