C++ Polymorphism and C Callbacks

Description

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?

nodemcu32s

What LVGL version are you using?

7.9.1

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

N/A

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

2 Likes

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