Quote:
#include <iostream>
using namespace std;
>
// A macro to define dummy functions:
#define DF(N) void N() { \
cout << "function " #N " called..." << endl; }
>
DF(a); DF(b); DF(c); DF(d); DF(e); DF(f); DF(g);
>
void (*func_table[])() = { a, b, c, d, e, f, g };
>
int main() {
while(1) {
cout << "press a key from 'a' to 'g' "
"or q to quit" << endl;
char c, cr;
cin.get(c); cin.get(cr); // second one for CR
if ( c == 'q' )
break; // ... out of while(1)
if ( c < 'a' || c 'g' )
continue;
(*func_table[c - 'a'])();
system("PAUSE");
}
} ///:~
>
This code has been copy-pasted from Eckel's Thinking in c++. I added
the system line so that I could test it on my Windows XP (Probably
Eckel would have preferred a different change since system ("PAUSE");
is not portable).
>
It does work, and I almost understand it, but not quite.
>
This is how I see it: {a, b, c, d, e, f, g} is an array of pointers
to functions.
>
This array is called func_table. So a is a pointer which = =
func_table[0], b is a pointer which == func_table[1] etc.
>
However, this is clearly wrong because a and b are actually functions,
not pointers at all.
>
My problem is that, on one hand, we seem to have an array of pointers
to functions.
>
Yet, on the other hand, these "pointers to functions" are called a, b,
c, d, e, f, g.
>
If they are pointers, shouldn't they have names which correspond to
addresses?
>
Perhaps, someone could clarify the key points.
>
[I _do_ understand how the macro concept is being applied. I
understand fully how the macro creates seven functions: void a(), void
b() etc., so no need to explain that part.]
>
Thank you very much,
>
Paul Epstein
to a pointer.
a, b ...
&a, &b...
It means the same.