-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconst_pointer.tex
88 lines (66 loc) · 1.7 KB
/
const_pointer.tex
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
\section{const}
\subsection{const pointer}
\begin{frame}[fragile]{const variablen}
\begin{block}{const für Konstanten}
\begin{lstlisting}[language=C++]
const int answer = 42;
answer = 99; // wird nicht kompiliert
int j = answer+5; // ist erlaubt
\end{lstlisting}
\end{block}
\end{frame}
\begin{frame}[fragile]{const pointer}
\begin{block}{const pointer}
\begin{small}
\begin{lstlisting}[language=C++]
// Pointer zu einem konstanten MyClass Objekt
const MyClass * blub;
MyClass const * blub2;
const MyClass const * blub3;
// konstanter Pointer zu einem MyClass Objekt
MyClass * const blub4;
// konstanter Pointer zu einem konstanten MyClass Objekt
MyClass const * const blub5;
const MyClass * const blub6;
const MyClass const * const blub7;
\end{lstlisting}
\end{small}
\end{block}
\end{frame}
\begin{frame}[fragile]{const Funktions-Parameter}
\begin{block}{const als Veränderungsschutz}
\begin{small}
\begin{lstlisting}[language=C++]
void drucker(int arg)
{
std::cout << arg << std::endl;
}
void testfun(int *arg)
{
*arg = 42;
}
void myfun(const int *arg)
{
*arg = 99; // wird nicht kompiliert
testfun(arg); // wird nicht kompiliert
drucker(*arg); // erlaubt
}
\end{lstlisting}
\end{small}
\end{block}
\end{frame}
\begin{frame}[fragile]{const Funktions-Parameter}
\begin{block}{const als Absicherung}
\begin{lstlisting}[language=C++]
void druckeRechnung(const Rechnung& rech)
{
std::cout << rech->menge << std::endl;
rech->menge++; // wird nicht kompiliert
}
void setzeRechnung(Rechnung& rech)
{
rech->menge = 100;
}
\end{lstlisting}
\end{block}
\end{frame}