如何使用boost :: bind绑定类成员函数? [英] How can I use boost::bind to bind a class member function?

查看:212
本文介绍了如何使用boost :: bind绑定类成员函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#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绑定类成员函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆