Ptr-to-member as callback in a c-function

R

rammel

Hi,

I'm using a c-library in my c++ project.
Therefore I've to call this c-function:
int rm_init(int id, char* ip, unsigned short port, int timeout,
callaback_fnc);

the signatures of the callback function is:
void callback_fnc(int, int, int, char*)

I want to use a class member as the callback-function and the call
shall be done in the ctor of the same class:

struct C
{
void cb(int id, int cmd, int len, char* data);

C()
{

}
};
 
R

rammel

Sorry I pressed enter to fast and forget the question:

how would I call rm_init() from C() and give the member funtions
(C::cb()) instead a c-funtion to it?
I guess I need some bind or mem_fun magic, but Im not shure about ist.

Thank you in advance!
 
J

John Harrison

rammel said:
Sorry I pressed enter to fast and forget the question:

how would I call rm_init() from C() and give the member funtions
(C::cb()) instead a c-funtion to it?
I guess I need some bind or mem_fun magic, but Im not shure about ist.

Thank you in advance!

Simple answer, you can't.

You must provide a global function for your callback. Your global
function can then call C::cb() if that is what you want to do. The C
library appears to have been badly designed so that this is not easy.

john
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Simple answer, you can't.

You must provide a global function for your callback. Your global
function can then call C::cb() if that is what you want to do. The C
library appears to have been badly designed so that this is not easy.

Actually it does not have anything to do with the C library, but rather
the difference between a function-pointer and an member-function-
pointer. If you want to call a member-function through a pointer you
must have a way to tell for which object you want to call the member-
function.
 
J

John Harrison

Erik said:
Actually it does not have anything to do with the C library, but rather
the difference between a function-pointer and an member-function-
pointer. If you want to call a member-function through a pointer you
must have a way to tell for which object you want to call the member-
function.

Sorry, I meant the C library that the OP is using. The API for that
library appears badly designed.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top