我想停止警告
在通话中:
pthread_create(th, NULL,
(void* (*)(void*)) &ClientHandler::handle,
(void *) clientHandler);
其中
handle()
是 ClientHandler
的成员函数:void* ClientHandler::handle();
我很难破译来自编译器的函数类型消息。
问题是:
handle()
接口(interface)吗? 我可以摆脱整体类型转换吗? 最佳答案
你不能直接这样做,指向成员函数的指针不是指向函数的普通指针,不能直接交给 C
回调。
您将需要一级间接:
void callHandle(void *data) {
ClientHandle *h = static_cast<ClientHandle*>(data);
h->handle();
}
pthread_create(th, 0, &callHandle, static_cast<void*>(handle));
有关更多信息/替代方案,请参阅 C++FAQ 的 Pointers to members 部分。
有关
callHandle
中类型转换的有效性,请参阅 this question 。当然,当 handle
被调用时,您全权负责确保 callHandle
仍然有效(以及它实际上指向 ClientHandle
的事实)。关于c++ - create_pthread() 调用的类型转换成员函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6826620/