Operator Overloading Flashcards
What is an example of an overloaded operator built into c++?
«_space;stream insertion/bitwise left shift operator
When do + and - perform differently?
Depending on arithmetic type (int, float, pointer)
What is an alternative to overloaded operators?
Explicit function calls
What namespace does string belong to?
std
What overloaded operators does string provide?
Equality, relational, assignment, addition assignment, and subscript
What is boolalpha?
Stream manipulator
Displays true or false instead of 1/0
How to indicate string object literal?
Place letter s immediately following closing “ of string literal
Member function of string that return substring?
substr
Which string overloaded operator does not perform bounds checking?
[]
How to bounds check with string?
Member function at returns reference if subscript is valid
How to overload an operator?
Function definition with keyword operator followed by operator being overloaded
Operators overloaded must be what type of member functions?
Non-static
Must be called on an object
Which operators can be used on class objects without being defined?
=, &, and ,
Can you change precedence or associativity by overloading?
No
The number of operands an operator takes can…..
Not be changed
Can you create new operators?
No
Can you change the meaning of how an operator works on fundamental types?
No
Which assignment operators must be declared as class members
(), [], ->
Binary operator must be overloaded as a ___________ member function with one arg or as a ______ member function with two args (with one of the arguments a _______)
non-static
non-
Class object or reference to a class object
Unary operators for a class can be overloaded as a __________ member function with no arguments or a _________ member function with one argument (which must be a _______)
non-static
non-
Object or reference to an object
Why should member functions that implement overloaded operators be non-static?
So they can access the non-static data in each object of the class
How to overload prefix and postfix operators?
Each must have a distinct signature.
Postfix must have second arg of type int for compiler to distinguish
Dynamic memory management
Controls allocation and deallocation of memory for built in or user defined type
Heap/free store
Memory assigned to a program to store dynamically allocated objects at execution time
What does new operator do?
Allocates right amount of storage for object
Runs objects constructor
Returns pointer of correct type
Throws exception if not enough memory
Delete operator
Destroys dynamically allocated object and frees space
How can you improve performance of non-member operator functions?
Declare as friend
How to obtain class arg of member functions explicitly?
The this pointer
How to enable an operator to be commutative?
Overload as non-member function
Conversion constructors
Turn objects of other types into objects of particular class
Ex: overloaded cast-operator functions
What can be used as a conversion constructor?
Constructor called with a single argument
Can a compiler call cast operators and conversion constructors ?
Implicitly when necessary
Explicit constructors
Can’t be used in an implicit conversion
Can you overload the function call operator? ()
Yes, allows arbitrary number of parameters