在我们打算用 C++ 编写的项目中,我们使用 C 库。该 C 库提供了注册回调的函数,这些函数在每次中断时调用。我们在构造函数中注册回调。所以我们基本上有以下(简化的)结构:
OurClass::OurClass() {
//the registerISRCallback is the C function, that accepts as first parameter the pin,
//the interrupt is expected on and as second parameter a function pointer:
//void (*callbackFunction)(int number, int time)
registerISRCallback(SCLK, handle_interrupt);
}
void OurClass::handle_interrupt(int pin, int time) {
//example: Blink a Led on the instance-LedPin
}
Run Code Online (Sandbox Code Playgroud)
然而问题是双重的:
void (OurClass::*)(int, int)。因此不可能这样使用它。有没有更多的解决方案可以在我们的类中使用这个 C API 函数并保持代码干净和可读?