Advanced Operators Flashcards
Advanced Operators
What are the ‘Big 3’ in C++?
- Copy CTOR
- Copy Assignment
- Destructor
Advanced Operators
What is the significance of the ‘Big 3’ in C++?
If you define one, you must define them all, as the defaults have been removed.
Advanced Operators
What is the syntax of a copy constructor given class MyClass?
MyClass (const MyClass &rhs) {do stuff}
Advanced Operators
What is the syntax of the copy assignment operator given class MyClass?
Class MyClass{
int width;
public:
MyClass& operator=(const MyClass &rhs) {
this->width = rhs.width;
return *this;
}
};
Advaned Operators
Why do Assignment Operator overloads return a reference?
Because the object itself is being modified.
Advaned Operators
When are copy CTOR / Assignment necesary?
When you are using pointers within a class.
Advaned Operators
What is the behavior of Default copy CTOR/Assignment?
To create a shallow copy whose pointers still reference the original object.
Advaned Operators
What is the syntax for a default Destructor? (DTOR)
~ClassName() = default;
Advanced Operators
When must DTORs be declared?
Any time that Dynamic Memory is handled, or the ‘new’ keyword is in a CTOR.
Advanced Operators
Which Operator Overloads require the Method/Function to return a reference rather than an instance?
- Assignment Operators (=, +=, -=, etc)
- Unary Operators (-, ++, –, etc)
Advanced Operators
How do you distinguish preincrement vs postincrement operator overloads?
The post increment is given an unnamed dummy argument. Ex:
objectname operator++(int) {
Advanced Operators
What does a postincrement Operator Overload Method/Function Return?
The state of the object prior to the increment via a copy.
Advanced Operators
Why is postincrement inadvisable?
Because a copy is returned rather than the original object, operator chaining can yield an incorrect answer.