fbcyborg
23-09-2009, 18:09
Salve a tutti,
stavo studiando sul libro Thinking in C++, e mi sono imbattuto nel seguente codice:
//: C03:FunctionTable.cpp
// Using an array of pointers to functions
#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'])();
}
} ///:~
Ho però un dubbio:
1) Ho visto che senza l'istruzione "cin.get(cr);", il programma non funziona bene. Suppongo che esso serva per il carriage return, ma non ho capito perché vada catturato.
stavo studiando sul libro Thinking in C++, e mi sono imbattuto nel seguente codice:
//: C03:FunctionTable.cpp
// Using an array of pointers to functions
#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'])();
}
} ///:~
Ho però un dubbio:
1) Ho visto che senza l'istruzione "cin.get(cr);", il programma non funziona bene. Suppongo che esso serva per il carriage return, ma non ho capito perché vada catturato.