where to define and declare non-member function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

I have a class Myclass:

in Myclass.h file:

class{
private:
    int sd;

public:
    void func(int sd, short op, void *ptr);

    void start();
};

in Myclass.cpp file:

void Myclass::start(){
     event_set(ev, sd, EV_READ, call_func, NULL ); //this is a library API, which trigger the callback of call_func(sd, op, NULL);
}

void Myclass::func(int sd, short op, void *ptr)){
    ...
    ...
}


in start(), the event_set need a function of "void(*func)()" type as argument,but func() is of "void Myclass::(*func)()" type, so I define a new function as below:

1
2
3
void call_func(int sd, short op, void *ptr){
    Myclass::func(int sd, short op, void *ptr);
}


however, I'm at a loss where to delcare and define call_func() so that the Myclass::start() can use call_func as argument and call_func() can call Myclass::func()
ptr is probably a context pointer. We want to pass the object address and then use the callback to fixup the call.
header file:
1
2
3
4
5
6
7
8
class
{
    int sd;

public:
    void func(int sd, short op); // don't need ptr here, so I removed it
    void start();
};

source file:
1
2
3
4
5
6
7
8
9
10
11
12
namespace
{
    void call_func(int sd, short op, void* ptr)
    {
        (Myclass*)ptr->func(sd, op);
    }
}

void Myclass::start()
{
    event_set(ev, sd, EV_READ, call_func, this);
}
Topic archived. No new replies allowed.