This usually happens when you need to squirrel your C ++ objects through the C API. A classic example is the thread class.
Here's the standard idiom for this:
thrd_hdl_t c_api_thread_start(int (*thread_func)(void*), void* user_data);
class my_thread {
public:
my_thread() hdl_(c_api_thread_start(my_thread::thread_runner,this)) {}
private:
virtual int run() = 0;
thrd_hdl_t hdl_;
static int thread_runner(void* user_data)
{
my_thread* that = static_cast<my_thread*>(user_data);
try {
return that->run();
} catch(...) {
return oh_my_an_unknown_error;
}
}
};
Will this help?
source
share