OOP C9 Overloaded operators
Overloading Operators allows user-defined types(class) to act like built-in
types. It is another way to make a function call.
Overloaded operators
-
Unary operators that can be overloaded:
!
++
--
operator
new
operatornew[]
operatordelete
operatordelete[]
-
Binary operators that can not be overloaded:
+
-
*
/
%
^
&
|
~
<<
>>
=
<
>
+=
-=
*=
/=
%=
^=
&=
|=
>>=
<<=
==
!=
<=
>=
&&
||
,
->*
->
()
[]
-
Operators that can’t be overloaded:
- 4 important:
.
.*
::
?:
sizeof typeid
static_cast
dynamic_cast
const_cast
reinterpret_cast
- 4 important:
Restrictions of overloading
- Only existing operators can be overloaded—— No creating new operators !
- Operators must be overloaded on a class or enumeration type
- Overloaded operators must preserve number of operands
- Overloaded operators must preserve precedence.
Overloaded Operator: A function
keyword : operator
, a prefix to name.
Function Type
The function could be a member function or a global / free function.
As member functions
1 |
|
-
Implicit first argument
-
No type conversion performed on receiver
1
2
3z = x + y //OK, x.operator+(y)
z = x + 3 //OK
z = 3 + y //Error! -
Developer Must have access to class definition
-
Members have full access to all data in class
As a member function, the object itself already provides one argument. Therefore,
- For binary operators (+, -, * .etc), member functions require one argument.
- For unary operators (unary -, ! .etc), member functions require no arguments.
As global functions
1 |
|
-
Explicit first argument
-
Type conversions performed on both arguments
1
2
3
4z = x + y;
z = x + 3;
z = 3 + y;
z = 3 + 7; -
Developer does not need special access to classes
-
Can be made a friend function
1
2
3
4
5
6
7
8class Integer {
friend const Integer operator+ (const Integer& lhs, const Integer& rhs);
...
}
const Integer operator+(const Integer& lhs, const Integer& rhs){
return Integer(lhs.i + rhs.i );
} -
For binary operators (+, -, * .etc), member functions require two argument.
-
For unary operators (unary -, ! .etc), member functions require one arguments.
If you don’t have access to private data members, then the global function must use the public interface (e.g. accessors)
Members vs. Free Functions
Unary operators
should bemember functions
.=
()
[]
->
->*
MUST BEMEMBER FUNCTION
!- All other
binary operators
should beglobal functions
.
Argument and Return values
Argument Passing
- read-only: pass it in as a
const reference
.(exceptbuilt-in
s) - don’t change the class: make member functions
const
,e.g
boolean, +, -.etc
- argument that might change: pass as
reference
.
Return values
Select the return type depending on the expected meaning of the operator.
- Logical operators should return
bool
orint
. Const
object so the result cannot be modified as anleft value
.
How to write?
prototypes
1 |
|
postfix and prefix, ++ and –
Postfix
forms take an argument, which is not used actually. It is only for distinguishing, complier will pass a 0.
1 |
|
prefix++ is more efficient for postfix++ call prefix++. Compliers will do the optimization automatically today.
1 |
|
Relational operators
1 |
|
1 |
|
Operator []
- Must be a member function, single argument.
- t should return a
reference
rather a pointer.
stream extractor >>
1 |
|
Therefore,
1 |
|
In this way, we can creating our own manipulators like
1 |
|
for instance,
1 |
|