I think the "cout" will be invoked once when the loop is invoked once. However the "cout" is invoked twice when the loop is invoked once.
Code:
#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);
void (*func_table[])()={a, b, c}; //define and initialize an array of pointers to functions
int main ()
{
while(1) {
cout<<"press a key from 'a' to 'b' or 'q' to quit"<<endl;
char c;
cin.get(c);
if (c=='q')
break; //...out of while (1)
if (c<'a' || c>'g')
continue;
(*func_table[c-'a'])(); //dereferencing calls the function
}
}
Here is the result:
[root@localhost abide]# g++ fun_poi_c.cpp -o fun_poi_c
[root@localhost abide]# ./fun_poi_c
press a key from 'a' to 'b' or 'q' to quit
a
functionacalled...
press a key from 'a' to 'b' or 'q' to quit
press a key from 'a' to 'b' or 'q' to quit
b
functionbcalled...
press a key from 'a' to 'b' or 'q' to quit
press a key from 'a' to 'b' or 'q' to quit
q
[root@localhost abide]#
What is the wrong? Thank you.