0

I'm using libuv in a C++ program. I have two classes, A and B that inherit from C.

I use libuv and declared an instance of uv_signal_t on C. Creating an instance of uv_signal_t requires a callback to be passed. I can easily pass a lambda to the C function to go around the problem of references to static member functions:

const int32_t r = uv_signal_start(&this->signal, [](uv_signal_t *handle, int signum){}, SIGABRT);

But how can I provide a different implementation of the callback on each child class? Ideally I'd have some common code implemented on C.cpp and additional code on each child.

Update

To be clear, I cannot change the signature of the callback method, as it's defined by libuv. I could edit the source for libuv, but I'm not sure I want to go that deep.

ruipacheco
  • 15,025
  • 19
  • 82
  • 138

2 Answers2

0

I'll add a function pointer as a property to my class C and implement it as necessary on the derived classes. More here.

ruipacheco
  • 15,025
  • 19
  • 82
  • 138
-1

I think you can capture this pointer in the lambda, and call a virtual function through this. The virtual function can be overrided in derived classes then.

 const int32_t r = uv_signal_start(&this->signal, [this](uv_signal_t *handle, int signum){ return this->callback(); }, SIGABRT);
Min Lin
  • 3,177
  • 2
  • 19
  • 32