I need to implement a structure (or class) where each instance has a “pointer” to a specific function. Is this possible?
Something like this:
struct constrain{
string name;
int direction;
double evaluate (vector <double> &x_var);
};
Where evaluate “points” to a specific function, so that when I create my constrain object I can tell to which function the object method evaluate should point to and when I later use it (e.g. my constrain objects will be contained within a std::vector) I can call the specific function.
Consider using
std::function:It’s better to pass
vectorby reference as pmr suggested. Here is the full example:If your
STLimplementation does not havestd::functionconsider usingboost::function