I need to call a method that expects a function pointer, but what I really want to pass to it is a functor. Here's an example of what I'm trying to do:
#include <iostream>
#include "boost/function.hpp"
typedef int (*myAdder)(int);
int adderFunction(int y) { return(2 + y); }
class adderClass {
public:
adderClass(int x) : _x(x) {}
int operator() (int y) { return(_x + y); }
private:
int _x;
};
void printer(myAdder h, int y) {
std::cout << h(y) << std::endl;
}
int main() {
myAdder f = adderFunction;
adderClass *ac = new adderClass(2);
boost::function1<int, int> g =
std::bind1st(std::mem_fun(&adderClass::operator()), ac);
std::cout << f(1) << std::endl;
std::cout << g(2) << std::endl;
printer(f, 3);
printer(g, 4); // Is there a way to get this to work?
}
I haven't been able to find a way to get the last line, printer(g, 4), to compile. Is there a way to get this to work? The only things in my control are the method "main" and the class "adderClass".