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

前端之家收集整理的这篇文章主要介绍了c – 如何使用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)

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

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

占位符允许您说“我只绑定某些参数;其他参数将在稍后提供.”

原文链接:https://www.f2er.com/c/116779.html

猜你在找的C&C++相关文章