I want to do the following:
class A{
private:
//some data
B b;
double f(double);
public:
A(){
b = new B(f); // does not work of course
}
};
class B{
public:
B(double (*func)(double));
};
Class B is supposed to solve a mathematical problem specified by the function func. Class A should now use B to solve this problem for func=f. The member function f accesses the private data members of A. The problem is, of course that I cannot simply pass a pointer to member function. I know there are ways to do that, but B should still be able to take any function, not only members of A. Until now, I just made f and the members of A static, but I think this is a rather bad design. Can you think of any workaround for this?
std::function
. If you need to shove an object forthis
into a member function, it's now easy to do so. If the member function doesn't usethis
, make it a free function orstatic
.std::function
looks fine, I will try that later. I'm not really into C++11, so maybe that's why I didn't find it.boost::function
existed before C++11 :)