如何将一个类的成员函数作为参数传递给另一类的成员函数?

问题描述 投票:0回答:1
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));

}

注意-emulNet是EmulNet类的对象

以上代码无效。

No viable conversion from '__bind<int (MP1Node::*)(void *, char *, int), MP1Node *>' to 'std::function<int (void *, char *, int)>'
c++ c++11 std-function
1个回答
1
投票

如果您坚持使用std::bind,请加入

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

或者,使用lambda:

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

(用上面的内容替换您的bind通话。

© www.soinside.com 2019 - 2024. All rights reserved.