对于create_pthread剧组成员函数()调用 [英] Cast member function for create_pthread() call

查看:179
本文介绍了对于create_pthread剧组成员函数()调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想停下来警告

server.cpp:823:警告:从'无效*(ClientHandler的:: )()来转换无效的()(无效的)

server.cpp:823: warning: converting from 'void* (ClientHandler::)()' to 'void ()(void)'

在呼叫:

pthread_create(th, NULL,
    (void* (*)(void*)) &ClientHandler::handle,
    (void *) clientHandler);

其中,手柄()是的成员函数 ClientHandler的

void* ClientHandler::handle();

我从编译器破译功能型的消息困难。

I have difficulties deciphering the function-type message from the compiler.

现在的问题是:


  • 我应该修改手柄()接口?我摆脱整体铸造的?

  • 我应该改变投?可究竟是什么?

  • 完全不同的东西?

  • Should I change the handle() interface? Can I get rid of casting overall?
  • Should I change the cast? To what exactly?
  • Something completely different?

推荐答案

您不能这样做直接指向成员函数不是纯函数指针,不能交给Ç 直接回调。

You can't do that directly, pointers to member functions are not plain pointers to functions and can't be handed over to C callbacks directly.

您将需要一个间接层:

void callHandle(void *data) {
  ClientHandle *h = static_cast<ClientHandle*>(data);
  h->handle();
}

pthread_create(th, 0, &callHandle, static_cast<void*>(handle));

查看成员指针的部分C ++ FAQ以了解更多信息/方案。

See the Pointers to members section of the C++FAQ for more information / alternatives.

有关投在 callHandle 的有效性,见<一href=\"http://stackoverflow.com/questions/310451/should-i-use-static-cast-or-reinter$p$pt-cast-when-casting-a-void-to-whatever\">this问题。你是唯一负责确保处理是仍然健在的时候 callHandle 被称为课程(并为事实上,它实际指向一个 ClientHandle )。

For the validity of the cast in callHandle, see this question. You are sole responsible for making sure that handle is still alive and well when callHandle is called of course (and for the fact that it actually points to a ClientHandle).

这篇关于对于create_pthread剧组成员函数()调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆