首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何将boost绑定与成员函数一起使用

如何将boost绑定与成员函数一起使用
EN

Stack Overflow用户
提问于 2010-02-21 07:37:19
回答 1查看 121.4K关注 0票数 81

以下代码导致cl.exe崩溃(MS VS2005)。

我正在尝试使用boost绑定来创建一个函数来调用myclass的方法:

代码语言:javascript
复制
#include "stdafx.h"
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <functional>

class myclass {
public:
    void fun1()       { printf("fun1()\n");      }
    void fun2(int i)  { printf("fun2(%d)\n", i); }

    void testit() {
        boost::function<void ()>    f1( boost::bind( &myclass::fun1, this ) );
        boost::function<void (int)> f2( boost::bind( &myclass::fun2, this ) ); //fails

        f1();
        f2(111);
    }
};

int main(int argc, char* argv[]) {
    myclass mc;
    mc.testit();
    return 0;
}

我做错了什么?

EN

回答 1

Stack Overflow用户

发布于 2022-01-29 11:28:10

下面的方法对我很有效。

代码语言:javascript
复制
class test_component
{
private:
    void on_wait_complete(const int i);
};

void test_component::on_wait_complete (const int i)
{
    cout << "on_wait_complete was called" << endl;

    return;
}

int main()
{
    // timerPtr_ is a variable declared in class test_component.
    timerPtr_->async_wait(boost::bind(&test_component::on_wait_complete, this, _1));
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2304203

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档