C++ Polymorphism and C Callbacks


I have a Hal class that I have made the flush method pure virtual. I also have a non-virtual instance method called run that does my display driver setup and is supposed to assign the c callback to the flush_cb property of the display driver.

How would you C++'ers go about making this assignment?

What MCU/Processor/Board and compiler are you using?


What LVGL version are you using?


What do you want to achieve?

Assign a member instance to the flush_cb.

What have you tried so far?

I’ve tried reinterpret_cast() and std::bind() to no avail.

Code to reproduce

The base class code: https://github.com/9ae8sdf76/dbuddy/blob/cpp-rewrite/src/hal.cpp
The concrete implementation: https://github.com/9ae8sdf76/dbuddy/blob/cpp-rewrite/src/nodemcu32s.cpp

Screenshot and/or video


I do this by storing the class instance pointer in user_data and using a non-member function to invoke the member one.


storing the class instance pointer

I decided to do an extern "C" function that uses a global pointer that I set right before setting the c callback. I think that’s pretty much what you’re achieving using user_data. And wouldn’t you know it, it was really that simple.

Thank you so much!

1 Like