annotation
I have a class that stores the optimization problem and runs the solver of this problem. If the solver fails, I want to consider the sub-problem and solve it using the same solver (and class).
Introduction
The optimization problem is essentially related to (mathematical) functions. The task functions are defined outside the class, but the subtask functions are defined inside the class, so they have different types (for example, void (*) and void (MyClass::*) .
At first I thought I could apply a member function to a pointer-type of non-member, but I found that I could not. Therefore, I am looking for another way.
Code example
Sample code to simulate my problem:
#include <iostream> using namespace std; typedef void (*ftype) (int, double); // Suppose foo is from another file. Can't change the definition void foo (int n, double x) { cout << "foo: " << n*x << endl; } class TheClass { private: double value; ftype m_function; void print (int n, double x) { m_function(size*n, value*x); } public: static int size; TheClass () : value(1.2), m_function(0) { size++; } void set_function (ftype p) { m_function = p; } void call_function() { if (m_function) m_function(size, value); } void call_ok_function() { TheClass ok_class; ok_class.set_function(foo); ok_class.call_function(); } void call_nasty_function() { TheClass nasty_class; // nasty_class.set_function(print); // nasty_class.set_function(&TheClass::print); nasty_class.call_function(); } }; int TheClass::size = 0; int main () { TheClass one_class; one_class.set_function(foo); one_class.call_function(); one_class.call_ok_function(); one_class.call_nasty_function(); }
As the example shows, a member function cannot be static. Also, I cannot override the original function of the problem to get the object.
Thanks for any help.
Edit
I forgot to mention. I tried switching to std :: function, but my original function has more than 10 arguments (this is a Fortran routine).
Decision
I made changes to std::function and std::bind as suggested, but did not redesign the function with more than 10 arguments. I decided to create an intermediate function. The following code illustrates what I did, but with fewer variables. Thanks to everyone.
#include <iostream>
PS. Creating a std::function with more than 10 variables seemed possible (compiled, but I have not tested) with
#define BOOST_FUNCTION_NUM_ARGS 15 #include <boost/function/detail/maybe_include.hpp> #undef BOOST_FUNCTION_NUM_ARGS
But creating std::bind for more than 10 arguments does not look so easy.