But you can get around it if, like many C libraries using callbacks,
the callback mechanism takes a user-defined data pointer. You can do
something like this (semi-pseudocode):
class MyClass
{
public:
returnType callbackFunction(paramType param);
};
extern "C"
{
// Function of the form required by the C library. For example:
returnType callbackFunction(paramType param, void* userData)
{
MyClass* instance = (MyClass*)userData;
return instance->callbackFunction(param);
}
}
namespace
{
MyClass myClassInstance;
}
void someFunctionSomewhere()
{
// Let's assume the C library function for setting the callback
// function has the form:
// void cLibSetCallbackFunc(returnType(*)(paramType, void*),
// void* userData);
cLibSetCallbackFunc(callbackFunction, &myClassInstance);
}
I'm guessing that in his case the signature of the callback function
does not include a void*.
The best way is to do the following:
//Top of cpp file that contains the place you are calling the C-
interface
//make sure this is in an anonymous namespace. You do NOT want any
other code accessing this
namespace
{
static ClassWithCallbackFunction* gCallbackInstance = NULL;
extern "C" void callbackWrapper(int callbackArg)
{
gCallbackInstance->CallbackFunc(callbackArg);
}
}
int main(int argc, char** argv)
{
gCallbackInstance = new ClassWithCallbackFunction();
c_func_that_requires_callback(callbackWrapper);
}
This is obviously bad for a lot of reasons, but if it's really the
only way for you to interface with a pre-existing library for which
there is no way to modify the code, then there aren't a whole lot of
options