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

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

问题描述

 的#include< QtCore / QCoreApplication>
#包括LT&;升压/ bind.hpp>
#包括LT&;升压/ function.hpp>类按钮
{
 上市:    提振::函数<无效()>点击时;
    提振::函数<无效(INT,双)GT; onClick2;
};级球员
{
 上市:
    无效播放(INT I,双O){}
    空隙停止(){}
};按钮为playButton,STOPBUTTON;
玩家thePlayer;无效连接()
{
    //错误C2298:上指针非法操作,以成员函数前pression:回归
    playButton.onClick2 =的boost ::绑定(安培;球员::播放,和放大器; thePlayer);
    stopButton.onClick =的boost ::绑定(安培;球员::停止,&安培; thePlayer);
}INT主(INT ARGC,CHAR *的argv []){    QCoreApplication一个(ARGC,ARGV);
    连接();
    返回a.exec();
}


解决方案

 的boost ::绑定(安培;球员::播放,和放大器; thePlayer)

您需要使用占位符两个参数:

 的boost ::绑定(安培;球员::播放,和放大器; thePlayer,_1,_2)

的占位符,您可以说:我只是结合某些参数;其他的参数将在稍后提供。

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

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