Operator Overloading in a hierarchy Flashcards
When do you use the “using” declaration?
If you want to bring down candidate values in the derived class from the base class with different arguments.
What are the absolute member operators within a hierarchy?
- [ ]
- =
- +=
- ++(prefix)
When calling an overloaded assignment operator to kickstart the base class’s assignment operator, what do the following operations do?
- *this = source_obj;
- (base)*this = source_obj);
- Does compile. BUT this is infinitely recursive and will call the derived class’s assignment operator repeatedly.
- Does not compile. This will have a syntax error because it is trying to type convert without a cast or upcasting. The error is will be “rvalue when expected lvalue” because it will attempt to make a type converted copy with a temporary variable.
How do you kickstart a base class’s operator from a derived class that has the same operator implemented?
- static_cast(*this) = 2nd_operand;
- base::operator = (2nd_operand);
- 2nd_operand.operator = (2nd_operand);
With FUNCTION overloading in C++, what happens when we have an operator with different arguments in the derived and base class and we try to use the base class’s operator in the derived class?
Syntax error. The compiler will find the operator in the derived class scope and cause an error because the parent’s operator is hidden.
What happens when operators of a derived class have the same name operators of their parent’s class?
Operators of a derived class “hide” their parent operators as would be expected with any member functions.
How do you call a base class’s member operator in a derived class?
- base::operator += (2nd_operand);
2. static_cast (*this) = 2nd_operand;
What are the non_member operators?
Any operator whose first operand is not an object of the class.
bit shifting operators: <>
conditional operators: , <=, >=, !=, ==
binary arithmetic operators: +, -, ++(postfix), / , %
Why is using “friends” for non-member operators okay for object-oriented programming?
Because it allows for direct access of the data members (which should be protected or private) without the use of setters and getters (which is not object-oriented).
What is the absolute case to make an operator a non-member?
The first operand is not an object of the class.
How do you call a non-member (friend) operator from a base class in a derived class?
friends are not inherited so, to call a non-member friend operator (or use it in some way) of the base class in the derived class you can:
1. use static cast to type convert the operator; operand_1 + static_cast(operand_2); OR 2. use the operator to act as a wrapper function to call a member function in the derived class that does the important stuff. Then call the member function. operand_2.function(operand_1);