Advanced Operators Flashcards

1
Q

Advanced Operators

What are the ‘Big 3’ in C++?

A
  1. Copy CTOR
  2. Copy Assignment
  3. Destructor
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Advanced Operators

What is the significance of the ‘Big 3’ in C++?

A

If you define one, you must define them all, as the defaults have been removed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Advanced Operators

What is the syntax of a copy constructor given class MyClass?

A

MyClass (const MyClass &rhs) {do stuff}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Advanced Operators

What is the syntax of the copy assignment operator given class MyClass?

A

Class MyClass{
int width;
public:
MyClass& operator=(const MyClass &rhs) {
this->width = rhs.width;
return *this;
}
};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Advaned Operators

Why do Assignment Operator overloads return a reference?

A

Because the object itself is being modified.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Advaned Operators

When are copy CTOR / Assignment necesary?

A

When you are using pointers within a class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Advaned Operators

What is the behavior of Default copy CTOR/Assignment?

A

To create a shallow copy whose pointers still reference the original object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Advaned Operators

What is the syntax for a default Destructor? (DTOR)

A

~ClassName() = default;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Advanced Operators

When must DTORs be declared?

A

Any time that Dynamic Memory is handled, or the ‘new’ keyword is in a CTOR.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Advanced Operators

Which Operator Overloads require the Method/Function to return a reference rather than an instance?

A
  • Assignment Operators (=, +=, -=, etc)
  • Unary Operators (-, ++, –, etc)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Advanced Operators

How do you distinguish preincrement vs postincrement operator overloads?

A

The post increment is given an unnamed dummy argument. Ex:
objectname operator++(int) {

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Advanced Operators

What does a postincrement Operator Overload Method/Function Return?

A

The state of the object prior to the increment via a copy.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Advanced Operators

Why is postincrement inadvisable?

A

Because a copy is returned rather than the original object, operator chaining can yield an incorrect answer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly