我需要做一个“函数调用器”的功能:它接收到一个通用的函数指针( void *
)和参数作为参数变量数,它有调用这个函数,传递的参数,和通用指针返回返回值。 然而,此项功能指针可以指向任何种类的功能(具有任何返回类型)中,即使具有恒定数目的参数的功能。 这将是这样的:
void * function_caller(void * function_pointer, ...) {
void * returning_value;
// Call the function and get the returning value
return returning_value; // this returning value will be properly casted outside
}
这样一来,下面的代码将工作:
int function1(int a, char b) {
// ...
}
void function2(float c) {
// ...
}
float function3() {
// ...
}
int main() {
int v1;
float v3;
v1 = *(int *) function_caller((void *) &function1, 10, 'a'); // Which would be equivalent to v1 = function1(10, 'a');
function_caller((void *) &function2, 3.0); // Which would be equivalent to function2(3.0);
v3 = *(float *) function_caller((void *) &function3); // Which would be equivalent to v3 = function3();
return 0;
}
我知道我将不得不使用一个va_list
,但我不知道如何通过传递参数的指针调用该函数。
所以,伙计们,什么想法?