C在声明/编译时带有参数的函数指针

问题描述 投票:0回答:2

假设我们有一个事件(int)和动作(函数指针)的列表,如:

typedef struct action_type {
    int event;
    int (*func)();
} action_type;

action_type actions[] = {
    { my_event1, my_action1 },
    { my_event2, my_action2 },
    { my_event3, my_action3 },
    { my_event4, my_action1 }
}

并且我想为表的一部分实际运行该操作添加条件,>]

  • 更好的可读性和
  • 简化动作功能。
action_type actions[] = {
    { my_event1, exceeds(&temperature, 100)   , my_action1 },
    { my_event1, between(&temperature, 50, 80), my_action2 },
    ...
}

有没有办法得到这样的符号?

或者我是否需要创建类似的内容:

action_type actions[] = {
    { my_event1, $temperature, exceeds, 100, my_action1 },
    ...
}

但是,这种方法只允许使用固定类型的固定数量的参数。当我编写库时,条件可能几乎是什么,我正在寻找一种方法,以允许对condition()函数使用不同的变量类型和不同的参数计数。


编辑:添加了有关参数数量和类型可能不同的条件的附加信息。

假设有事件(int)和动作(函数指针)的列表,如:typedef struct action_type {int event; int(* func)(); } action_type; action_type actions [] = {{my_event1,...

c parameters function-pointers
2个回答
2
投票

我会使用X_MACRO,即:类似这样的东西:


0
投票

您可以做这样的事情

© www.soinside.com 2019 - 2024. All rights reserved.