Arrays of pointers to functions
One of the more interesting constructs you can create is an array of
pointers to fuArrays of pointers to functionsnctions. To select a function, you just index into the
array and dereference the pointer. This supports the concept of
table-driven code; instead of using conditionals or case statements,
you select functions to execute based on a state variable (or a
combination of state variables). This kind of design can be useful if
you often add or delete functions from the table (or if you want to
create or change such a table dynamically).
The following example creates some dummy functions using a
preprocessor macro, then creates an array of pointers to those
functions using automatic aggregate initialization. As you can see,
it is easy to add or remove functions from the table (and thus,
functionality from the program) by changing a small amount of
code:
//: C03:FunctionTable.cpp
// Using an array of pointers to functions
#include <iostream>
3: The C in C++ 217
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'])();
}
} ///:~
At this point, you might be able to imagine how this technique
could be useful when creating some sort of interpreter or list
processing program
One of the more interesting constructs you can create is an array of
pointers to fuArrays of pointers to functionsnctions. To select a function, you just index into the
array and dereference the pointer. This supports the concept of
table-driven code; instead of using conditionals or case statements,
you select functions to execute based on a state variable (or a
combination of state variables). This kind of design can be useful if
you often add or delete functions from the table (or if you want to
create or change such a table dynamically).
The following example creates some dummy functions using a
preprocessor macro, then creates an array of pointers to those
functions using automatic aggregate initialization. As you can see,
it is easy to add or remove functions from the table (and thus,
functionality from the program) by changing a small amount of
code:
//: C03:FunctionTable.cpp
// Using an array of pointers to functions
#include <iostream>
3: The C in C++ 217
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'])();
}
} ///:~
At this point, you might be able to imagine how this technique
could be useful when creating some sort of interpreter or list
processing program
本文介绍了一种使用函数指针数组的技术,通过该技术可以基于状态变量选择执行不同的函数,从而实现表驱动代码的设计。文章提供了一个示例程序,演示了如何创建一系列虚拟函数,并通过数组来管理和调用这些函数。
1245

被折叠的 条评论
为什么被折叠?



