0

I have a static function Bar::Function(std::string s1, std::string s2, std::string s3 ) that I want to pass as a functionpointer to the constructor of class Foo which has a boost::function member.

But the three strings are created in the Foo() class and not yet known when the function is passed as a functionpointer to the Foo's constructor. Should I simply pass three empty strings as I'm doing it in the main()?

class Foo(boost::function<int(std::string, std::string, std::string)> fFunction)
{
public:
    Foo()
    {
        fnCallback = fFunction;
    }

    int Call()
    {
        return fnCallback("first", "second", "third")
    }
protected:
     boost::function<int(std::string, std::string, std::string)> fnCallback;
};


int main(int /*nArgc*/, char */*paszArgv*/[])
{
    boost::function<int(std::string, std::string, std::string)> fn = boost::bind(Bar::Function, "", "", "");
    Foo oFoo(fn);
    oFoo.Call();
    return 0;
}

class Bar
{
public:
    static int Function(std::string s1, std::string s2, std::string s3)
    {
        std::cout << s1 << s2 << s3 << std::endl;
    }
};

EDIT: This is how the code should look. The function pointer only holds the adress of the function. Arguments are passed when it is called!

fn = &Bar::Function;
dragosht
  • 3,237
  • 2
  • 23
  • 32
tzippy
  • 6,458
  • 30
  • 82
  • 151
  • When you pass a function pointer, it goes without argument. – YSC Dec 21 '15 at 10:02
  • Take a look at placeholders http://stackoverflow.com/questions/18680607/role-of-placeholder-in-boostbind-in-the-following-example – dkg Dec 21 '15 at 10:05
  • I posted sample code. btw, there are lots of grammar error... – yasuharu519 Dec 21 '15 at 10:15
  • This question is a classical example of an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). You don't need to use a `boost::function` for this. (In fact, you don't want to use a `boost::function` for this.) A simple function pointer will suffice. – David Hammen Dec 21 '15 at 11:08

2 Answers2

2

Here is a minimal example:

#include <iostream>
#include <string>

struct Foo
{
    typedef int (*callback_type)(std::string, std::string, std::string);

    Foo(callback_type callback)
        : _callback(callback)
    {}

    int operator()()
    {
        return _callback("1", "2", "3");
    }

private:
     callback_type _callback;
};

struct Bar
{
    static int Function(std::string s1, std::string s2, std::string s3)
    {
        std::cout << s1 << s2 << s3 << std::endl;
        return 0;
    }
};

int main()
{
    Foo foo(Bar::Function);
    foo();
    return 0;
}

Prints 123\n.

YSC
  • 38,212
  • 9
  • 96
  • 149
1

I think you need to use std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, instead of blank strings. If me, I would do like following,

https://gist.github.com/yasuharu519/213307b4709662e60df2

#include <functional>
#include <iostream>
#include <string>

class Foo
{
public:
  Foo(std::function<int(std::string, std::string, std::string)> fFunction) : fnCallback{fFunction} {}
  int Call() { return fnCallback("first", "second", "third"); }

protected:
  std::function<int(std::string, std::string, std::string)> fnCallback;
};

class Bar
{
public:
  static int Function(std::string s1, std::string s2, std::string s3)
  {
    std::cout << s1 << s2 << s3 << std::endl;
    return 0;
  }
};

int main(int /*nArgc*/, char* /*paszArgv*/ [])
{
  using namespace std::placeholders;
  auto fn = std::bind(Bar::Function, _1, _2, _3);
  Foo oFoo(fn);
  oFoo.Call();
  return 0;
}
yasuharu519
  • 244
  • 2
  • 8
  • `Bar::Function` is a static function. There is no need for placeholders : `Foo oFoo(Bar::Function)`. – YSC Dec 21 '15 at 10:21
  • I am not the down voter; this is technically correct. It is however overkill. There's no need for using `std::bind` or placeholders in this case. – David Hammen Dec 21 '15 at 12:09
  • While true in the specific case shown here, it quite likely that the OP has simplified their code to ask the question (this is a good thing!) - but have unwittingly simplified it so much that they cleanest answer is changed. As such, placeholders may be *exactly* what they need! – Martin Bonner supports Monica Dec 21 '15 at 12:29