我们可以使用函数指针调用函数吗?如果是的话怎么办?
ramu
问问题
332 次
4 回答
13
是的。简单的例子:
// Functions that will be executed via pointer.
int add(int i, int j) { return i+j; }
int subtract(int i, int j) {return i-j; }
// Enum selects one of the functions
typedef enum {
ADD,
SUBTRACT
} OP;
// Calculate the sum or difference of two ints.
int math(int i, int j, OP op)
{
int (*func)(int i, int j); // Function pointer.
// Set the function pointer based on the specified operation.
switch (op)
{
case ADD: func = add; break;
case SUBTRACT: func = subtract; break;
default:
// Handle error
}
return (*func)(i, j); // Call the selected function.
}
于 2008-10-31T04:16:48.620 回答
4
是的。这是一个带有示例的好教程。
于 2008-10-31T04:19:30.750 回答
2
是的你可以。
于 2008-10-31T04:13:37.067 回答
1
是的。一个例子:
代码前...
typedef int ( _stdcall *FilterTypeTranslatorType ) ( int TypeOfImportRecord, PMA 类型 *PMA ); FilterTypeTranslatorType FilterTypeTranslator = {NULL};
现在在代码中...
PMA类型 *PMA; 处理 hFilterDll; // 假设 DLL 已加载 // 现在找到地址... ... FilterTypeTranslator[TheGroup] = ( FilterTypeTranslatorType ) GetProcAddress( hFilterDll, "FilterTypeTranslator" ); ... // 现在调用它 FilterTypeTranslator(1,PMA); ...
于 2008-10-31T04:18:52.937 回答