[英]Add a parameter to a function pointer
我正在使用函数指针来委托 C 中的状态。
// states
void state1(int first, int second) {
...
}
void state2(int first, int second) {
...
}
// state pointer
void (*current_state)(int, int);
// state logic
if (condition1) {
current_state = &state_1;
} else if (condition2) {
current_state = &state_2;
}
// do the thing
(*current_state)(1, 2);
现在我处于给定参数不再足够的地步,因此需要第三个参数。 由于我不想更改所有状态,我想知道是否可以将准常量参数与指针一起传递。 像这样的东西:
void state3(int first, int second, int third) {
...
}
// state logic
...
else if (condition3) {
// calculate the constant third argument
int param3 = 0;
current_state = &state_3(int, int, param3);
}
我有办法让它发挥作用吗?
好吧,我不确定我会推荐它,但是您可以执行以下操作:
#include <stdlib.h>
#include <stdio.h>
typedef int pfun();
int alice( int a, int b) { return b; }
int bob( int a, int b, int c) { return c; }
int main( )
{
pfun* f;
f = alice;
printf( "%d\n", f( 1, 2, 3));
f = bob;
printf( "%d\n", f( 1, 2, 3));
return EXIT_SUCCESS;
}
这与
gcc -o funp -Wall funp.c
没有警告,并且运行正常。
关键是像
int f();
说f是一个返回int的函数,带有未指定的参数。 不利之处在于,编译器无法检查参数是否为正确的类型。
您可以传递*void
指针并将其强制转换为状态函数中的具体类型。 所有状态功能将具有相同的签名。 像这样:
// states
struct state1_args {
int first;
int second;
};
struct state2_args {
float first;
float second;
float third;
};
void state1(void* state_args) {
struct state1_args* args = (struct state1_args*)state_args;
use(args->first);
...
}
void state2(void* state_args) {
struct state2_args* args = (struct state2_args*)state_args;
use(args->third);
...
}
// state pointer
void (*current_state)(void*);
// state logic
if (condition1) {
current_state = &state_1;
current_state_args = &args_1;
} else if (condition2) {
current_state = &state_2;
current_state_args = &args_2;
}
// do the thing
(*current_state)(current_state_args);
与您的签名相同
// state pointer
void (*current_state)(int, int);
这不可能。 您必须更改状态签名,或者更改如何从state3中删除参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.