1

I have consult (disclaimer):

To illustrate my problem I will use this code (UPDATED)

#include <iostream>
#include <cmath>
#include <functional>


class Solver{
public:
    int Optimize(const std::function<double(double,double>& function_to_optimize),double start_val, double &opt_val){
        opt_val = function_to_optimize(start_val);
        return 0;
    }
};

class FunctionExample{
public:
    double Value(double x,double y)
    {
        return x+y;
    }
};

int main(){

    FunctionExample F =FunctionExample();
    Solver MySolver=Solver();

    double global_opt=0.0;

    MySolver.Optimize(std::bind(&FunctionExample::Value, &F, std::placeholders::_2),1,global_opt);

    return 0;
}

Is there a way to call the method "Value"? I have no problems to call a function (without a class)

typedef double (*FunctionValuePtr)(double x);

But this does not help me with the example above. I need the explicit method name. Most examples use a static method. I can not use a static method.

Community
  • 1
  • 1
  • 1
    Read about [`std::function`](http://en.cppreference.com/w/cpp/utility/functional/function) and [`std::bind`](http://en.cppreference.com/w/cpp/utility/functional/bind). – Some programmer dude Feb 05 '14 at 16:08
  • Also, the declaration `Square S();` (and the other two like it) does not do quite what you expect them to do. In the case of `S` it actually declares a *function* `S` which takes no arguments and return an instance of the `Square` class. – Some programmer dude Feb 05 '14 at 16:17

1 Answers1

1

You can use the <functional> header of the STL:

double Gradient(const std::function<double(double)>& func, double y)
{
    const double h = 1e-5;
    return (func(y+h) - func(y)) / h;
}

std::cout << D.Gradient(std::bind(&Root::Value, &R, std::placeholders::_1), 8) << std::endl;

Also like Joachim Pileborg commented you are declaring functions in main, so you need to remove the ().

Edit:

To give bind a fixed argument you can do the following:

int Optimize(const std::function<double(double)>& function_to_optimize, double &opt_val){
    opt_val = function_to_optimize(opt_val);
    return 0;
}

MySolver.Optimize(std::bind(&FunctionExample::Value, &F, std::placeholders::_1, 1), global_opt);

This will call F.Value(opt_val, 1). You can also swap the placeholder with the fixed argument.

typ1232
  • 5,535
  • 6
  • 35
  • 51
  • But there is no way to pass only a function without a classname? –  Feb 05 '14 at 16:33
  • @wieschoo Member functions always need a class instance to operate on. Could you maybe update your question with an example that is closer to your problem and why my solution would not work for you? – typ1232 Feb 05 '14 at 16:38
  • Is it better to have pointer to a class and a method? Something like using the object "R" and the code "double Eval(R,Value,1,2)" I want to use different classes that all have a method "Value". –  Feb 05 '14 at 16:58
  • I updated the example that is more likely to my real situation. –  Feb 05 '14 at 17:07
  • @wieschoo Ok but what is the question? Isn't your original question of how to call the member function solved? – typ1232 Feb 05 '14 at 17:10
  • My original intension was that a solver method just gets a function oracle (with some arguments that are fixed(in number of arguments and types of arguments)). Only the reference to the method of a class providing the value of the oracle call should be flexible. –  Feb 05 '14 at 17:13
  • @wieschoo But you still need a class instance to execute a member function. I'll add to my answer how to give bind fixed arguements. – typ1232 Feb 05 '14 at 17:18