Operator Overloading Flashcards

1
Q

Operator Overloading

What is operator overloading?

A

Defining certain operators as your own custom Functions / Methods

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

Operator Overloading

What are the two options for operator overloading?

A

A method and a function

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

Operator Overloading

What are the 3 rules of Operator Overloading?

A
  1. Must define all desired relationships. (int + class) != (class + int)
  2. extensible not mutable (i.e cant override existing overloads)
  3. Strict domain of valid operators
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Operator Overloading

What are the characteristics of using a Function to overload and operator?

A
  • It is free floating and not tied to a class.
  • Must define both sides of the operator
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Operator Overloading

What are the characteristics of using a Method to overload and operator?

A
  • Built into the class itself
  • Only define the right side of the operator
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Operator Overloading

When would you use a Function to overload an operator?

A

When the left side of the operator is a primitive or pre-defined type.

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

Operator Overloading

When would you use a Method to overload and operator?

A

When the left side of the operator can be represented by the ‘This’ keyword.

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

Operator Overloading

What is the syntax for using a Function to overload an operator?

A

return_type operator (const type& lhs, const type& rhs) {

}

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

Operator Overloading

What is meant by operator overloading not being mutable?

A
  • You cannot change an already defined relationship.
  • You cannot add functionality between two primitives/fundamental types.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Operator Overloading

When overloading an operator, why are function arguments const?

A

Because an operator should not change its operands, but return a new object.

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

Operator Overloading

What are the main operators that cannot be overloaded?

A
  • sizeof
  • typeid
  • scope resolution (::)
  • ternary operators (? :)
  • member acces operators (., .*)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly