You cannot convert a non-stationary member function to a pointer to a function even with the same signature, because technically a member function has a hidden parameter called this . One solution is to make listen_uv_listen_uv_connection_cb static:
class _tcp { uv_tcp_t* tcp = NULL; public: ~_tcp() { delete tcp; } static void listen_uv_listen_uv_connection_cb(uv_stream_t* stream, int status) { printf("NEW CONNECTION\n"); } void listen(const char* host, int port) { tcp = new uv_tcp_t(); uv_tcp_init(uv_default_loop(), tcp); sockaddr_in* addr = new sockaddr_in(); uv_ip4_addr(host, port, addr); uv_tcp_bind(tcp, (const sockaddr*)addr, 0); delete addr; uv_listen((uv_stream_t*)tcp, TCP_BACKLOG, &_tcp::listen_uv_listen_uv_connection_cb); } };
PS, in order to be able to call a non-static method, you will need a way to get a pointer to your _tcp instance from the parameter "uv_stream_t * stream". I would suggest using the "void * uv_handle_t.data" pointer from this document http://docs.libuv.org/en/latest/handle.html#c.uv_handle_t
static void listen_uv_listen_uv_connection_cb(uv_stream_t* stream, int status) { _tcp *tcp = static_cast<_tcp *>( stream->data ); tcp->regularMethod(); }
Of course, you must assign this pointer to uv_handle_t.data when initializing uv_tcp_t * :
void listen(const char* host, int port) { tcp = new uv_tcp_t(); uv_tcp_init(uv_default_loop(), tcp); tcp->data = this;
and I would move this initialization code to the constructor.
You will need such a static shell for each callback that you intend to use with this library. With C ++ 11, you can probably use lambda instead.
Slava source share