简体   繁体   中英

Member function pointers for subclass

I have one class SFAPin, and there's one method:

SFAPin::addListener(void (SFAState::*)(SFAPinEvent&), uint8_t){};

But when I tried pass one method of another class for this method:

void InitialState::lightChange(SFAPinEvent &event){};

...

light.addListener(&lightChange, SFAPinEvent::PIN_CHANGE);

I got:

error: no matching function for call to 'SFAPin::addListener(void (InitialState::*)(SFAPinEvent&), uint8_t)' note: candidates are: void SFAPin::addListener(void (SFAState::*)(SFAPinEvent&), uint8_t) Obs: InitialState extends SFAState, but don't work

I'm using arduino IDE and don't support lambda of C++11

How can I resolve it? Thanks

A pointer to member of derived is not convertible to a pointer to member of base. If it were, you could do things like (baseInstance.*methodOfDerived)() which doesn't make sense. The covariance goes the other way round: you could take a pointer to member of base, and call it on an instance of derived.

It's not quite clear what you are trying to achieve, but your current approach ain't going to fly. You need to reconsider your design.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM