papaiatis papaiatis - 1 day ago 7
C++ Question

How to properly pass a pointer to a member function for a 3rd party code?

I have this 3rd party type definition:

typedef void (*NexTouchEventCb)(void *ptr);


Which is used in a function in the 3rd party library:

void NexTouch::attachPop(NexTouchEventCb pop, void *ptr)


If I have a single .cpp file then I can successfully call the
attachPop
without any compiler errors:

NexButton b0 = NexButton(0, 1, "b0");

void b0PopCallback(void *ptr)
{
// do something here
}

b0.attachPop(b0PopCallback, &b0);


But I do OOP, so I have my own header and class files:

MyNextion.h

class MyNextion {
public:
void init();
void b0PopCallback(void *ptr);
private:
NexButton *b0;
}


MyNextion.cpp

#include "MyNextion.h"

void MyNextion::init() {
b0 = new NexButton(0, 1, "b0");
b0->attachPop(/* what should I put here? */);
}

void MyNextion::b0PopCallback(void *ptr) {
// do something here
}


I went through several similar questions like this, this and this one as well. But I just don't get it.

Note that this code is compiled for Arduino, which is a AVR microcontroller. Special C++ syntaxes may not be available.

Can you please tell me how should I call
b0->attachPop
and why should I call that way?

Please note, that I'm not able to modify the 3rd party library, which can be found here: https://github.com/itead/ITEADLIB_Arduino_Nextion

Answer

You can only use a static member function (or non-member function) in this way. Fortunately, your API passes an additional void* pointer where you can put the object you want to call on:

class MyNextion {
public:
    void init();
    void b0PopCallback();
private:
    static void b0PopStaticCallback(void *ptr);
    NexButton *b0;
};

void MyNextion::b0PopStaticCallback(void *ptr)
{
    static_cast<MyNextion*>(ptr)->b0PopCallback();
}

void MyNextion::init()
{
    b0 = new NexButton(0, 1, "b0");
    b0->attachPop(b0PopStaticCallback, this);
}
Comments