简体   繁体   中英

How can I pass member function of one class as an argument to member function of another class?

class EmulNet
{
    public:
    int ENrecv(Address *myaddr, std::function<int (void*,char*,int)> fn, struct timeval *t, int 
    times, void *queue); 
};

class MP1Node{
    public:
    int recvLoop();
    int enqueueWrapper(void *env, char *buff, int size);
};

int MP1Node::recvLoop() {
    return emulNet->ENrecv(&(memberNode->addr), std::bind(&MP1Node::enqueueWrapper,this), NULL, 1, &(memberNode->mp1q));

}

Note - emulNet is an object of class EmulNet

Above code doesn't work.

No viable conversion from '__bind<int (MP1Node::*)(void *, char *, int), MP1Node *>' to 'std::function<int (void *, char *, int)>'

If you insist on using std::bind , make it

std::bind(&MP1Node::enqueueWrapper,this,
    std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)

Alternatively, use a lambda:

[this](void *env, char *buff, int size) { return enqueueWrapper(env, buff, size); }

(replace your bind call with the above).

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