30.06.2013 Aufrufe

Softwareentwicklung in C++ - ASC

Softwareentwicklung in C++ - ASC

Softwareentwicklung in C++ - ASC

MEHR ANZEIGEN
WENIGER ANZEIGEN

Sie wollen auch ein ePaper? Erhöhen Sie die Reichweite Ihrer Titel.

YUMPU macht aus Druck-PDFs automatisch weboptimierte ePaper, die Google liebt.

470 15. Verschiedenes<br />

15.3 Funktionspo<strong>in</strong>ter<br />

Wie bereits <strong>in</strong> Abschnitt 6.2.4 erwähnt, gibt es auch <strong>in</strong> C ++ die bereits<br />

von C her bekannten Funktionspo<strong>in</strong>ter. Man kann e<strong>in</strong>fach e<strong>in</strong>e entsprechende<br />

Po<strong>in</strong>tervariable def<strong>in</strong>ieren und ihr als Adresse die E<strong>in</strong>sprungadresse<br />

e<strong>in</strong>er Funktion zuweisen. In C ++ kann man Funktionspo<strong>in</strong>ter allerd<strong>in</strong>gs<br />

nicht nur auf Funktionen zeigen lassen, sondern unter anderem auch auf<br />

Member-Methoden. Sehen wir uns hierzu e<strong>in</strong>fach e<strong>in</strong> kle<strong>in</strong>es Beispiel an<br />

(function_po<strong>in</strong>ter_demo1.cpp):<br />

1 // function po<strong>in</strong>ter demo1 . cpp − f i r s t demo f o r function p o i n t e r s<br />

2<br />

3 #<strong>in</strong>clude <br />

4 #<strong>in</strong>clude ” u s e r t y p e s . h”<br />

5<br />

6 us<strong>in</strong>g std : : cout ;<br />

7 us<strong>in</strong>g std : : endl ;<br />

8<br />

9 //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−<br />

10 u<strong>in</strong>t32 aCalculationFunction ( u<strong>in</strong>t32 val )<br />

11 {<br />

12 return ( val ∗ 5 ) ;<br />

13 }<br />

14<br />

15 //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−<br />

16 u<strong>in</strong>t32 anotherCalculationFunction ( u<strong>in</strong>t32 val )<br />

17 {<br />

18 return ( val + 17);<br />

19 }<br />

20<br />

21 //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−<br />

22 /∗<br />

23 ∗ CalcMethodCollection<br />

24 ∗<br />

25 ∗ j u s t a dummy c o l l e c t i o n of c a l c u l a t i o n methods<br />

26 ∗<br />

27 ∗/<br />

28<br />

29 class CalcMethodCollection<br />

30 {<br />

31 private :<br />

32 CalcMethodCollection ( ) ;<br />

33 CalcMethodCollection ( const CalcMethodCollection &) {}<br />

34 const CalcMethodCollection& operator = ( const CalcMethodCollection&)<br />

35 { return (∗ this ) ; }<br />

36 public :<br />

37 //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−<br />

38 static u<strong>in</strong>t32 aSpecialCalculationFunction ( u<strong>in</strong>t32 val )<br />

39 {<br />

40 return ( val ∗ 2 ) ;<br />

41 }<br />

42<br />

43 //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−<br />

44 static u<strong>in</strong>t32 anotherSpecialCalculationFunction ( u<strong>in</strong>t32 val )<br />

45 {<br />

46 return ( val + 12);<br />

47 }<br />

48 } ;<br />

49<br />

50<br />

51 //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−<br />

52 <strong>in</strong>t ma<strong>in</strong> ( <strong>in</strong>t argc , char ∗ argv [ ] )

Hurra! Ihre Datei wurde hochgeladen und ist bereit für die Veröffentlichung.

Erfolgreich gespeichert!

Leider ist etwas schief gelaufen!