-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06-operator-overloading.tex
More file actions
258 lines (227 loc) · 7.67 KB
/
06-operator-overloading.tex
File metadata and controls
258 lines (227 loc) · 7.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
\input{definitions}
\usepackage{colortbl}
\begin{document}
\title{Operator overloading}
\subtitle{Using standard library functions and providing an intuitive interface}
\date{\today}
\author{Florian Warg, Max Staff}
\maketitle
\begin{frame}[fragile]
\frametitle{How do you sort your own objects?}
\begin{itemize}
\item do not use your own containers
\item do not build your own sorting function
\item use the standard library
\visible<2->{\item solution: build own comparison operators}
\visible<3->{\item also helps for making your object "printable"...}
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Declaring an operator I}
\begin{lstlisting}[numbers=none]
class Gear {
float gearRatio;
public:
float getRatio() { return this->gearRatio; }
bool operator<(const Gear& other);
bool operator==(const Gear& other);
bool operator<=(const Gear& other);
bool operator>=(const Gear& other);
bool operator>(const Gear& other);
bool operator!=(const Gear& other);
}
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Declaring an operator II}
\begin{lstlisting}[numbers=none]
class Matrix {
int content[width * height];
public:
int* operator[](std::size_t index);
Matrix operator+(const Matrix& other) const;
Matrix& operator+=(const Matrix& other);
}
Matrix a, b;
a += b;
Matrix c = a + b;
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Declaring an operator III}
\begin{lstlisting}[numbers=none]
class Matrix {
int content[width * height];
public:
Matrix& operator++();
Matrix operator++(int);
Matrix& operator--();
Matrix operator--(int);
}
Matrix a;
++a;
a++;
--a;
a--;
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Overloading an increment operator}
\begin{lstlisting}[numbers=none]
Matrix& Matrix::operator++() {
for (auto& i : this->content) {
++i; // increment
}
return *this; // return incremented state
}
Matrix Matrix::operator++(int) {
Matrix tmp(*this); // copy
this->operator++(); // increment
return tmp; // return previous state
}
Matrix a;
++a;
a++;
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Overloading a function operator}
\begin{lstlisting}[numbers=none]
class Matrix {
int content[width * height];
public:
Matrix& operator()(std::size_t x, std::size_t y);
}
int Matrix::operator()(std::size_t x, std::size_t y) {
return this->content[y * width + x];
}
Matrix a;
int b = a(5, 3);
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Functors}
\begin{itemize}
\item Functor = object with overloaded \lstinline{()}-operator
\item better optimizable than function-pointers
\item useful with templates (coming soon)
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Which operators are overloadable?}
\begin{lstlisting}[numbers=none]
+ - * / %
+= -= *= /= %=
& | ^ ~
&= |= ^=
<< >> < > ==
<<= >>= <= >= !=
! && || ++ --
->* -> , () []
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Which operators are not overloadable?}
\begin{itemize}
\item \lstinline{::} scope resolution
\item \lstinline{.} member access
\item \lstinline{.*} member access through pointer to member
\item \lstinline{?:} ternary conditional
\visible<2->{\item new or nonexistent operators (\lstinline{**, <>, &|})}
\visible<3->{\item \lstinline{&&, ||} lose short-circuit (aka "lazy") evaluation}
\visible<4->{\item precedence, grouping or amount of operands cannot be changed}
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Overloading outside of the class}
\begin{tabular}{lll}
\rowcolor{gray!50}
expression & member function & non-member\\
{\lstinline!@a!} & {\lstinline!(a).operator@()!} & {\lstinline!operator@(a)!} \\
\rowcolor{gray!25}
{\lstinline!a@b!} & {\lstinline!(a).operator@(b)!} & {\lstinline!operator@(a, b)!} \\
{\lstinline!a=b!} & {\lstinline!(a).operator=(b)!} & \cellcolor{red!15}not possible \\
\rowcolor{gray!25}
{\lstinline!a(b...)!} & {\lstinline!(a).operator()(b...)!} & \cellcolor{red!25}not possible \\
{\lstinline!a[b]!} & {\lstinline!(a).operator[](b)!} & \cellcolor{red!15}not possible \\
\rowcolor{gray!25}
{\lstinline!a->!} & {\lstinline!a.operator->()!} & \cellcolor{red!25}not possible \\
{\lstinline!a@!} & {\lstinline!a.operator@(0)!} & {\lstinline!operator@(a, 0)!} \\
\end{tabular}
\end{frame}
\begin{frame}[fragile]
\frametitle{Example}
\begin{lstlisting}[numbers=none]
class Matrix {
int content[width * height];
// ...
public:
void print(std::ostream& stream) const;
}
std::ostream& operator<<(std::ostream& stream, const Matrix& matrix) {
matrix.print(stream);
return stream;
}
Matrix a, b;
std::cout << a << b << "\n";
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{When to overload}
Three Basic Rules of Operator Overloading in C++
\begin{itemize}
\item Only overload if the meaning of the operator is obviously clear and undisputed
\item Always stick to the operator's well-known semantics
\item Always provide all out of a set of related operations
\end{itemize}
Source: \href{http://stackoverflow.com/q/4421706/3729508}{http://stackoverflow.com/q/4421706/3729508}
\end{frame}
\begin{frame}[fragile]
\frametitle{How to overload}
The Decision between Member and Non-member
\begin{itemize}
\item Some operators are required to be members
\item unary operators should be members
\item if one operand is treated specially (e.g. is modified) should be members
\item if both operands are treated equally (e.g. addition) should be non-member
\item if access to private attributes is required should be non-member
\item try to avoid declaring friend functions
\end{itemize}
Source: \href{http://stackoverflow.com/q/4421706/3729508}{http://stackoverflow.com/q/4421706/3729508}
\end{frame}
\begin{frame}[fragile]
\frametitle{Which Comparison Operators to use}
\begin{itemize}
\item standard library algorithms (e.g. \lstinline{std::sort}) will always expect \lstinline{operator<}
\item users expect other operators if \lstinline{operator<} exists
\end{itemize}
\footnotesize
\begin{lstlisting}[numbers=none]
bool operator==(const X& l, const X& r){ /* actually compare */ }
bool operator!=(const X& l, const X& r){return !operator==(l,r);}
bool operator< (const X& l, const X& r){ /* actually compare */ }
bool operator> (const X& l, const X& r){return operator< (r,l);}
bool operator<=(const X& l, const X& r){return !operator> (l,r);}
bool operator>=(const X& l, const X& r){return !operator< (l,r);}
\end{lstlisting}
Source: \href{http://stackoverflow.com/q/4421706/3729508}{http://stackoverflow.com/q/4421706/3729508}
\end{frame}
\begin{frame}[fragile]
\frametitle{Conversion Operators}
\begin{itemize}
\item conversion to other types (e.g. bool, string) can be overloaded
\item difference between implicit and explicit conversion
\end{itemize}
\footnotesize
\begin{lstlisting}[numbers=none]
class MyString {
public:
operator const char*() const; {return data_;} // implicit
explicit operator const char*() const {return data_;}
private:
const char* data_;
}
\end{lstlisting}
Source: \href{http://stackoverflow.com/q/4421706/3729508}{http://stackoverflow.com/q/4421706/3729508}
\end{frame}
\end{document}