Part 4 - Operator overloading, Friend Functions, The Big Three Flashcards
How will the compiler treat ++t when searching for the operator overload?
How is this different to t++?
What is the implication of this?
++t:
t.operator++() or operator++(t)
t++:
t.operator++(0) or operator++(t, 0)
To overload the postfix version we need to write a function with an extra int argument
Why should the += operator return a reference to the object that has been assigned to?
so it can be used as part of a larger expression, e.g
(t+=7).printStandard()
or myt = t+=7;
Why can the prefix version of operator++ return a reference while the postfix has to return a copy?
Prefix version increments and then returns, so the value before incrementing is not needed by the programmer
Postfix version returns the value before it has been incremented so needs to copy, increment original, return copy
In the operator+ function, why would
Time tCopy(*this);
be preferred over
Time tCopy = *this;
Time tCopy = *this; would use the no-arg constructor and overwrite the default values
Time tCopy(*this) instead uses the copy constructor
When must a friend function be used in operator overloading?
Where the left hand side operand is not the class itself, e.g. << and the operator needs to access private or protected members of the class
friend operator overloads take both parameters as
a) references
b) pointers
a) references
Where must the friend declaration be made?
Within the class (.cpp) declaration
To have a reference to a const variable, a ___________ must be used
const reference
Why is the following code fragment illegal:
const int months = 12;
int &a = months;
a = 13;
What should be done to fix it?
It will result in a const variable being changed
const int &a = months;
a=13;
A constant member function is not allowed to return a ________ reference to the object to which it is applied or any of the members of that object
non-constant
What 3 things must each class have?
copy constructor
assignment operator
destructor
If the programmer fails to provide one of the “big three” functions what will happen?
The compiler will generate a default version of whatever is missing
How would the assignment operator for class X be declared?
X& operator=(const X&)
How is the destructor named for class X?
~X()
How is the copy constructor for class X declared?
X::X(const X&){…}
Why is the argument to the copy constructor always a reference?
Because otherwise call by value would be used - which uses the copy constructor to create the copy of the object
What will the default copy constructor generated by the compiler do?
Use the copy constructors for any members of the class which are objects of other classes and initialise all other members using simple copying
What will the default assignment operator do?
Just perform assignments to all members
What will the default destructor do?
Invoke the destructors for any members of the class which are objects of other classes
What is the typical circumstance that a non default version of one of the big three is required?
Why?
When the class has a pointer member
The default one will make the pointer point to the same object. Typically you will want it to instead refer to a copy of the object