関数ポインタを使用して関数を呼び出すことはできますか?はいの場合はどうですか?
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、
PMAType * PMA
);
FilterTypeTranslatorType FilterTypeTranslator = {NULL};
今コードで...
PMAType * PMA;
ハンドルhFilterDll;
//DLLがロードされていると想定
//ここでアドレスを見つけます...
..。
FilterTypeTranslator [TheGroup] =
(FilterTypeTranslatorType)GetProcAddress(hFilterDll、
"FilterTypeTranslator");
..。
//今それを呼びます
FilterTypeTranslator(1、PMA);
..。
于 2008-10-31T04:18:52.937 に答える