Skip to content

Commit 22c7860

Browse files
Add a slide on overloading
Co-authored-by: Stephan Hageboeck <[email protected]>
1 parent e44947b commit 22c7860

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

talk/basicconcepts/functions.tex

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,31 @@
200200
\end{block}
201201
\end{frame}
202202

203+
\begin{frame}[fragile]
204+
\frametitlecpp[98]{Overloading}
205+
\begin{block}{Overloading}
206+
\begin{itemize}
207+
\item We can have multiple functions with the same name
208+
\item They must have different parameter lists
209+
\item A different return type alone is not allowed
210+
\item Default arguments can cause ambiguities
211+
\item All functions with the same name form an ``overload set''
212+
\end{itemize}
213+
\end{block}
214+
\begin{exampleblock}{}
215+
\begin{cppcode*}{gobble=6}
216+
int sum(int b); // 1
217+
int sum(int b, int c); // 2, ok, overload
218+
// float sum(int b, int c); // disallowed
219+
sum(42); // calls 1
220+
sum(42, 43); // calls 2
221+
int sum(int b, int c, int d = 4); // 3, overload
222+
sum(42, 43, 44); // calls 3
223+
sum(42, 43); // error: ambiguous, 2 or 3
224+
\end{cppcode*}
225+
\end{exampleblock}
226+
\end{frame}
227+
203228
\begin{frame}[fragile]
204229
\frametitlecpp[98]{Functions}
205230
\begin{exercise}{Functions}

0 commit comments

Comments
 (0)