Pointer to Function Member

I am writing a program for minimization. It has two classes. Class A which does the minimization and Class B which prepares the objective function to be minimized. I need to pass the objective function provided by Class B as a member function to Class A, then the member function will be minimized.

I need to know what is the most efficient way to do that. Particularly, I have compilation errors regarding passing the function from class B to class A.

Any comments?

Thank you in advance,
mmgh

 
Any comments?

Comments on what? You haven't posted any code to comment on.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
template <class T>
class A
{
  private:
    std::vector<T>  p_;
    T*                     function_;

  public:
    A(std::vector<T>  p_, T* function_);
   ~A();

    void  minimize();   
};

template <class T>
A::A(std::vector<T>  p, T* function)
{
    P_ = p;
    function_ = function
}

template <class T>
void A<T>::minimize()
{
  /* minimize the "function" by optimizing parameter vector "p" */
}

class B
{
  private:
     /* some member variables*/
  public:
    B();
    ~B();

    double objective_func(double P[]);
    void  do_minimization();
};

void  B::do_minimization()
{
   double (*pFunc)(double) = &classB::objective_func;
   double* pObjFunc = (double*&) pFunc;
   classA<double> min(pObjFunc, p);
   min.minimize();
}
Last edited on
Something like this, perhaps?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <functional>
#include <vector>

template <class value_type>
struct A
{
    using func_type = std::function<value_type(std::vector<value_type>&)>;

    A(std::vector<value_type>& p, func_type f) : _params(p), _func(f)
    {
    }

    void  minimize() {}

private:
    std::vector<value_type>& _params;
    func_type _func;
};

struct B
{
    /* some member variables*/
    using param_type = std::vector<double>;

    double objective_func(param_type&) { return 0.0; }

    void  do_minimization()
    {
        auto func = [&](param_type& P) { return objective_func(P); };

        A<double>(p, func).minimize();
    }

private:
    param_type p;
};


Obviously simplified code, but one wonders why A is a class and not a function and why it needs access to the function if it only manipulates the vector.
Thank you so much.
It is the simplified code, because the actual code is too long. There are lots of different functions in each of the classes.

Actually, the minimize function changes one element of P per iteration and pass the new P to the objective_func to see if the new change minimizes the function or not.

for example: objective_func receives a vector P1 and then updates P using P1 to return the results. This will be done thousands of time to find the optimized P.

Based on your implementation, the objective function is receiving the address of the member variable P.

Best,
M
Last edited on
Topic archived. No new replies allowed.