Usually such libraries use an extra void *
argument that is passed to the function that takes the function pointers and is passed back to all the callback functions. If you have that, you can use it to pass your object pointer, casting it to void and back:
class MyObject;
void errCallback(void *m, short errcode, char *errmsg) {
static_cast<MyObject *>(m)->error(errcode, errmsg);
}
:
MyObject *m = new MyObject();
callLibrary(..., errCallback, m, ...);
If the library doesn't give you that extra argument, you have a problem -- the only other way to get extra data into the callback function is to use a global variable:
static MyObject *m;
void errCallback(short errcode, char *errmsg) {
m->error(errcode, errmsg);
}
:
m = new MyObject();
callLibrary(..., errCallback, ...);
The problem here being that you need to declare a new callback function (and global var) for each distinct object that you want to have receiving callbacks. If you create many such objects dynamically, that becomes unwieldy to manage.