Код: Выделить всё
extern "C"
{
/**
* This is part of my data provider's C API (which I cannot change)
* It asks me to put my data handler in a function, then register
* it as a C-style function pointer.
*/
void set_update_callback(void (*p)(struct Data *));
}
/** I tried to encapsulate this data service within a class */
class MyDataClass
{
private:
/** Data should pushed to this queue.
* The queue is created outside my class, then its pointer is passed in from the constructor (below)
*/
queue* _data_out_queue;
public:
MyDataClass(queue* data_out_queue): _data_out_queue(data_out_queue) {}
/** Subscribe to the data provider using my handler function */
void subscribe()
{
set_update_callback(handle_data);
}
/**
* This handler function is where the dilemma is:
* - It needs to be static because it needs to be converted to a plain C-style function pointer
* - It needs to be non-static because it needs to write to the member object _data_out_queue
*/
static void handle_data(struct Data *d)
{
// parse data from *d and push into _data_out_queue
}
}
Подробнее здесь: https://stackoverflow.com/questions/793 ... an-old-api