【发布时间】:2013-04-17 07:35:16
【问题描述】:
我希望通过指针将可变数量的参数传递给也被指针引用的回调函数。有什么方法可以创建一个可以通过引用传递的参数列表?
例如:
typedef struct MENUELEMENT
{
void* OK_func; void* OK_args;
} menuElement_t;
menuElement_t* curMenuElement;
menuElement_t menu[] =
{
//First Menu Element
(void*)menuDisplayText, (void*)("Test", (char*)&lcdBuffer[0]) //menuDisplayText is a function that takes two arguments
//Second Menu Element
(void*)menuDisplayVal, (void*)&value[0] //menuDisplayVal is a function that takes one argument
};
void loop() //Main Loop - just an example of how the function pointed to by curMenuElement is executed
{
curMenuElement = &menu[0];
if(KP_OK)
{
(*reinterpret_cast<void (*)(...)>(curMenuElement->OK_func))(curMenuElement->OK_args); //General template for function pointed to at OK_func with OK_args
}
}
到目前为止,这适用于一个参数,但是我无法弄清楚如何在 struct 变量的初始化中传递多个参数的列表。这甚至可以在不必使用使用 va_list 的构建器函数的情况下实现吗?
【问题讨论】:
-
这里没有 C。只是 C++... 和 what is
void main()?.. 和 why shouldn't you use it? -
将函数指针强制转换为
void*是非法的。 -
@JesseGood 确实如此。 lambda 的类型是
void *在哪一天和哪一天?那将是一个极其混乱的时代! -
@JesseGood:绝对正确,尽管符合 POSIX 的系统要求函数指针和
void*由于dlsym()function 而可以互换 -
FWIW - 这是一个嵌入式系统,只是一个例子来说明我想要完成的概念。如果您对文体有任何见解,而不是 cmets,那将不胜感激!
标签: c++ pointers arguments function-pointers