Hi, I'm creating a C++ wrapper to API written in C. One of the C function takes a pointer to function as an argument: cfunction(int (*callback)(int, *char[])); The wrapper uses a pure virtual function: class Process { public: virtual int run(int i, *char[]) = 0; }; Problem is, how to "convert" run to "callback"? I've tried cfunction(&Process::run), but g++ keeps complaining: cannot convert `int (Process::*)(int, char**)' to `int (*)(int, char**)' for argument `2' to `s_m_process* MSG_process_create(const char*, int (*)(int, char**), void*, s_m_host*)' -- -- Verdi March --
Verdi March wrote: One of the C function
takes a pointer to function as an argument: cfunction(int (*callback)(int, *char[]));
The wrapper uses a pure virtual function: class Process { public: virtual int run(int i, *char[]) = 0; };
Problem is, how to "convert" run to "callback"? I've tried cfunction(&Process::run), but g++ keeps complaining:
The basic problem here AFAIK is that C++ treats pointers to functions and pointers to member functions differently. As it stands &Process::run will return a pointer to member function. This cannot be used in place of a pointer to a function. Hence the compile error. If I recall correctly, it would be OK if Process::run were static. But a function can't be both virtual and static. STL uses member function adapters to allow you to call member functions as if they were not member functions, but, I think this relies on STL functions being overloaded to allow either functors or function pointers as arguments. Since C can't use functors, I think this method is not possible here. It would try a different method. I suspect that you could do something here. Maybe give each Process subclass a static function called run, give Process a static function pointer, initialised to zero and make each subclass initialiser point the static function pointer to a static function. That way, given a base object pointer p, you could probably use p->run in the way you want. -- JDL Non enim propter gloriam, diuicias aut honores pugnamus set propter libertatem solummodo quam Nemo bonus nisi simul cum vita amittit.
Hi, non-static member functions needs the this pointer. You cannot call them without an object. On Monday 09 June 2003 10:43, Verdi March wrote:
Hi,
I'm creating a C++ wrapper to API written in C. One of the C function takes a pointer to function as an argument: cfunction(int (*callback)(int, *char[]));
The wrapper uses a pure virtual function: class Process { public: virtual int run(int i, *char[]) = 0;
static int runCaller( Process* obj, int i, char** v) { return obj->run( i, v); }
};
Pointers to static member functions are like C-function pointers.
Problem is, how to "convert" run to "callback"? I've tried cfunction(&Process::run), but g++ keeps complaining:
cannot convert `int (Process::*)(int, char**)' to `int (*)(int, char**)' for argument `2' to `s_m_process* MSG_process_create(const char*, int (*)(int, char**), void*, s_m_host*)'
Thanks to all. So, instead of letting customization via inheritance, I simply add a method to class Process that set the proper function, i.e Process::setCustomFunction(int (*f)(int, char*[])); -- -- Verdi March --
Verdi March wrote:
Thanks to all.
So, instead of letting customization via inheritance, I simply add a method to class Process that set the proper function, i.e Process::setCustomFunction(int (*f)(int, char*[]));
I think you can use inheritance if you did something like the following. (The private base class constructor has a similar effect to using a pure virtual fucntion.) class Process { private: typedef int (*callback)( int, char** ); protected: Process( const callback run ) : run( run ){} public: const callback run; }; class Test : public Process { public: Test() : Process( &Test::run ){} private: static int run( int x, char** c ){ ... } }; This should let you use inheritance if you want. For example you could create an instance with: Process* process = new Test(); and then call cfunction( process->run ); and that should work. You could even make a subclass singleton if that's appropriate. OTOH, this could be a rather longwinded way of creating a number of similar functions, especially if you don't actually need polymorphism. -- JDL Non enim propter gloriam, diuicias aut honores pugnamus set propter libertatem solummodo quam Nemo bonus nisi simul cum vita amittit.
participants (3)
-
John Lamb
-
Sebastian Huber
-
Verdi March