【问题标题】:using boost::function with instance methods将 boost::function 与实例方法一起使用
【发布时间】:2013-11-15 01:40:30
【问题描述】:

我正在尝试通过以下示例将 boost::function 与实例方法一起使用

class someclass
{
public:

    int DoIt(float f, std::string s1)
    {
        return 0;
    }

    int test(boost::function<int(float, std::string)> funct)
    {
         //Funct should be pointing to DoIt method here
         funct(12,"SomeStringToPass");
    }

    void caller()
    {
                test(DoIt); //Error : 'someclass::DoIt': function call missing argument list; use '&someclass::DoIt' to create a pointer to member
    }
};

关于如何解决此问题的任何建议?

【问题讨论】:

  • 如果可以的话,使用std::functionstd::bind...

标签: c++ boost-function


【解决方案1】:

你应该使用boost::bind:

#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <string>
#include <iostream>

using namespace std;

class someclass
{
public:

    int DoIt(float f, std::string s1)
    {
        return 0;
    }

    int test(boost::function<int(float, std::string)> funct)
    {
        return funct(5.0, "hello");
    }

    void caller()
    {
        cout << test(boost::bind(&someclass::DoIt, this, _1, _2)) << endl;
    }
};

int main() {
    someclass s;
    s.caller();
}

【讨论】:

  • 谢谢你这样做 - 计时器后标记为答案
  • 没问题 Rajeshwar,您能将此答案标记为正确答案吗?它是左边的勾号。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-27
  • 2011-12-15
  • 2016-05-30
  • 2017-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多