c++ - Pass class function to another class function -


sorry possible duplicates, didn't understand examples , codes snippets found.

i have class named "encoderwrapper" includes functions. 1 of these functions called "onaftertouch" , declared in "encoderwrapper.h" file.

void onaftertouch(byte channel, byte pressure); 

the functions become callback class function of library use

inline void sethandleaftertouch(void (*fptr)(uint8_t channel, uint8_t pressure)) {                     usb_midi_handleaftertouch = fptr; }; 

note: i'm totally new c++, want sorry if i'm doing "no-gos" or mixing terms.

the question is: how can pass class function (member function?) "sethandleaftertouch" function of library?

this won't work:

void encoderwrapper::attachmidievents() {     usbmidi.sethandleaftertouch(&encoderwrapper::onaftertouch); } 

... ide says

no matching function call usb_midi_class:sethandleaftertouch(void (encoderwrapper::*)(byte, byte))

i've tried

usbmidi.sethandleaftertouch((&this->onaftertouch)); 

but won't work ... , don't approach on that.

every appreciated ;-)

function pointer , member function pointer have different types. can yourself:

struct test {     void fun(); };  int main() {     void(*ptr)() = &test::fun; // error! } 

instead, member function pointer need syntax:

void(test::*fun)() = &test::fun; // works! 

why ask? because member function need instance called with. , calling function have special syntax too:

test t;  (t.*funptr)(); 

to accept member function pointer, you'll need change code this:

inline void sethandleaftertouch(void(encodewrapper::*fptr)(uint8_t, uint8_t)) {                     usb_midi_handleaftertouch = fptr; }; 

since it's rather limiting accepting functions 1 class, recommend using std::function:

inline void sethandleaftertouch(std::function<void(uint8_t, uint8_t)> fptr) {                     usb_midi_handleaftertouch = std::move(fptr); }; 

this allow send lambda captures, , call member function insode it:

//  capture use member function inside //                           v--- usbmidi.sethandleaftertouch([this](uint8_t, channel, uint8_t pressure) {     onaftertouch(channel, pressure); }); 

it seems can't change, , looking @ api, doesn't seem have access state object.

in case, if want use member function, need introduce global state:

// global variable encodewrapper* encode = nullptr;  // in function sets handle encode = this; //            v--- no capture makes convertible function pointer usbmidi.sethandleaftertouch([](uint8_t, channel, uint8_t pressure) {     encode->onaftertouch(channel, pressure); }); 

another solution make onaftertouch function static. if it's static, it's pointer not member function pointer, normal function pointer.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -