|
|
|
|
|
|
|
Analysis: On line 17, pFunc is declared to be a pointer to a function returning void and taking two parameters, both of which are integer references. On line 9, PrintVals is declared to be a function taking three parameters. The first is a pointer to a function that returns void but takes two integer reference parameters, and the second and third arguments to PrintVals are integer references. The user is again prompted which functions to call, and then on line 33 PrintVals is called. |
|
|
|
|
|
|
|
|
Go find a C++ programmer and ask him what this declaration means: |
|
|
|
|
|
|
|
|
void PrintVals(void (*)(int&, int&),int&, int&); |
|
|
|
|
|
|
|
|
This is the kind of declaration that you will use infrequently and will probably look up in the book each time you need it, but it will save your program on those rare occasions when it is exactly the required construct. |
|
|
|
|
|
|
|
|
Using typedef with Pointers to Functions. |
|
|
|
|
|
|
|
|
The construct void (*) (int&, int&) is cumbersome, at best. You can use typedef to simplify this, by declaring a type VPF as a pointer to a function returning void and taking two integer references. Listing 20.8 rewrites the beginning of Listing 20.7 using this typedef statement. |
|
|
|
|
|
|
|
|
LISTING 20.8 USINGtypedefTO MAKE POINTERS TO FFUNCTIONS MORE READABLE |
|
|
|
 |
|
|
|
|
1: // Listing 20.8. using typedef
2:
3: #include <iostream.h>
4:
5: void Square (int&,int&);
6: void Cube (int&, int&);
7: void Swap (int&, int &);
8: void GetVals(int&, int&);
9: typedef void (*VPF) (int&, int&) ;
10: void PrintVals(VPF,int&, int&);
11:
12: int main()
13: {
14: int valOne=1, valTwo=2;
15: int choice;
16: bool fQuit = false;
17:
18: VPF pFunc;
19:
20: while (fQuit == false)
21: {
22: cout << (0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: ;
23: cin >> choice; |
|
|
|
 |
|
|
|
|
continues |
|
|
|
|
|