fork download
  1. #include <vector>
  2. #include <string>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. struct constrain{
  8. typedef double (*callback_t) (constrain *obj, vector <double> &x_var);
  9. string name;
  10. int direction;
  11. callback_t evaluate_f;
  12.  
  13. // helper function
  14. double evaluate(vector <double> &x_var) {
  15. return evaluate_f(this, x_var);
  16. }
  17. };
  18.  
  19. double func1(constrain *obj, vector<double> &p) {
  20. cout << obj->name << ", " << obj->direction << ", " << p.size() << endl;
  21. }
  22.  
  23. int main() {
  24. constrain ob;
  25. ob.name = "o";
  26. ob.direction = 1;
  27. vector<double> v(5);
  28.  
  29. ob.evaluate_f = &func1;
  30. ob.evaluate_f(&ob, v);
  31. ob.evaluate(v);
  32. }
Success #stdin #stdout 0.02s 2812KB
stdin
Standard input is empty
stdout
o, 1, 5
o, 1, 5