Part 4 - Operator overloading, Friend Functions, The Big Three Flashcards

1
Q

How will the compiler treat ++t when searching for the operator overload?

How is this different to t++?

What is the implication of this?

A

++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

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

Why should the += operator return a reference to the object that has been assigned to?

A

so it can be used as part of a larger expression, e.g

(t+=7).printStandard()

or myt = t+=7;

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

Why can the prefix version of operator++ return a reference while the postfix has to return a copy?

A

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

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

In the operator+ function, why would

Time tCopy(*this);

be preferred over

Time tCopy = *this;

A

Time tCopy = *this; would use the no-arg constructor and overwrite the default values

Time tCopy(*this) instead uses the copy constructor

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

When must a friend function be used in operator overloading?

A

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

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

friend operator overloads take both parameters as

a) references
b) pointers

A

a) references

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

Where must the friend declaration be made?

A

Within the class (.cpp) declaration

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

To have a reference to a const variable, a ___________ must be used

A

const reference

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

Why is the following code fragment illegal:

const int months = 12;

int &a = months;

a = 13;

What should be done to fix it?

A

It will result in a const variable being changed

const int &a = months;

a=13;

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

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

A

non-constant

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

What 3 things must each class have?

A

copy constructor

assignment operator

destructor

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

If the programmer fails to provide one of the “big three” functions what will happen?

A

The compiler will generate a default version of whatever is missing

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

How would the assignment operator for class X be declared?

A

X& operator=(const X&)

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

How is the destructor named for class X?

A

~X()

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

How is the copy constructor for class X declared?

A

X::X(const X&){…}

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

Why is the argument to the copy constructor always a reference?

A

Because otherwise call by value would be used - which uses the copy constructor to create the copy of the object

17
Q

What will the default copy constructor generated by the compiler do?

A

Use the copy constructors for any members of the class which are objects of other classes and initialise all other members using simple copying

18
Q

What will the default assignment operator do?

A

Just perform assignments to all members

19
Q

What will the default destructor do?

A

Invoke the destructors for any members of the class which are objects of other classes

20
Q

What is the typical circumstance that a non default version of one of the big three is required?

Why?

A

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

21
Q
A