简体   繁体   English

如何使用boost :: bind绑定类成员函数?

[英]How can I use boost::bind to bind a class member function?

#include <QtCore/QCoreApplication>
#include <boost/bind.hpp>
#include <boost/function.hpp>

class button
{
 public:

    boost::function<void()> onClick;
    boost::function<void(int ,double )> onClick2;
};

class player
{
 public:
    void play(int i,double o){}
    void stop(){}
};

button playButton, stopButton;
player thePlayer;

void connect()
{
    //error C2298: 'return' : illegal operation on pointer to member function expression 
    playButton.onClick2 = boost::bind(&player::play, &thePlayer);
    stopButton.onClick = boost::bind(&player::stop, &thePlayer);
}

int main(int argc, char *argv[])

{

    QCoreApplication a(argc, argv);
    connect();
    return a.exec();
}
boost::bind(&player::play, &thePlayer)

You need to use placeholders for the two arguments: 您需要为两个参数使用占位符:

boost::bind(&player::play, &thePlayer, _1, _2)

The placeholders allow you to say "I'm only binding certain arguments; other arguments will be provided later." 占位符允许您说“我仅绑定某些参数;稍后将提供其他参数”。

如果要创建可移植的代码,请直接指定占位符的名称空间:

boost::bind( &player::play, &thePlayer, ::_1, ::_2 ); // Placeholders of boost::bind are placed in global namespace.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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