C++ Chapter 13: Overloading and Templates
Which operators require that their overloading function be a member of the class?
() [ ] -> =
Which operators CANNOT be overloaded?
. .* :: ?: sizeof
When overloading an operator, what cannot be changed?
1) Precedence of the operator 2) Associativity of the operator 3) number of operands the operator takes (binary stays binary)
Can you redefine how operators work with simple data types?
NO. All interactions with predefined data types (such as int, bool, double, etc) cannot be redefined.
Does C++ allow the creation of new operators?
NO. only existing operators can be overloaded.
When must an operator overloading function be a friend function?
When the far left operand of the operator is an object of a different type from the class.
template
a C++ feature that enables the programmer to write generic code for functions and classes
typical placement of friend function prototypes
at the beginning of the class definition, before any member declarations.
parameterized types
class templates are called parameterized types because, based on the parameter type, a specific class is generated
syntax to overload the assignment operator (=)
const className& operator=(const className&);
for what objects can operators be overloaded?
either for user-defined objects or for a combination of user-defined and predefined data types.
The functions to overload >> and << must be...
friend functions of the class
Syntax to overload the stream extraction operator (>>)
friend istream& operator>>(istream&, className&);
General syntax to overload the stream insertion operator (<<)
friend ostream& operator<<(ostream& osObject, const className& cObject);
Where should the friend function definition be placed?
in the implementation file, without the class name and scope resolution operator
How to declare a friend function
include the keyword "friend" in front of the function prototype in the class definition.
If an operator function is a member function of a class, then the leftmost operand of that operator...
must be an object of that class
this
pointer that is used to indicate a class object as a whole, rather than one of its members.
syntax for operator function
returnType operator=(const className&) const; ex. IntArray& operator=(const IntArray& x)
*this
the address of the object indicated by "this"