如何将void*转换为函数指针?

Wil*_*iam 0 c++ function-pointers freertos

我在FreeRTOS中使用xTaskCreate,其第四个参数(void*const)是传递给新线程调用的函数的参数.

void __connect_to_foo(void * const task_params) {
   void (*on_connected)(void);
   on_connected = (void) (*task_params);
   on_connected();
}

void connect_to_foo(void (*on_connected)(void)) {
   // Start thread
   xTaskCreate(
         &__connect_to_foo,
         "ConnectTask",
         STACK_SIZE,
         (void*) on_connected, // params
         TASK_PRIORITY,
         NULL // Handle to the created Task - we don't need it.
         );
}
Run Code Online (Sandbox Code Playgroud)

我需要能够传入一个带签名的函数指针

void bar();

但我无法弄清楚如何将void*转换为我可以调用的函数指针.我能得到的最近的是:

错误:'void*'不是第3行的指针对象类型

如何将task_params转换为我可以调用的函数指针?

注意,上面的代码大大简化了.

Igo*_*nik 7

这些方面的东西:

typedef void (*OnConnected_t)();
OnConnected_t on_connected = OnConnected_t(task_params);
on_connected();
Run Code Online (Sandbox Code Playgroud)