Reference to member function

2019-09-07 13:36发布

问题:

first of all I know that this is not possible in C++. But I hope someone can tell be a workaround for my problem. I have a class which represents a mathematical function:

class myClass:
{
private:
public:
    myClass() {};
    double value(double, double){ /* doing some complicated calculation here */} };
    double integrate { /*calc*/ return integral; };
}

In integrate() I want to create a struct with a reference to value(). The struct is defined as follows:

struct gsl_monte_function_struct {
    double (*f)(double * x_array, size_t dim, void * params);
    size_t dim;
    void * params;
};

(I need this struct to call the Monte-Carlo integration routines from GSL)

As said before I know that this is forbidden in C++. But is there any possibility to use gsl_monte_function_struct with a member function of myClass? If it is not possible that myClass can integrate itself, is it possible to call gsl_monte_function_struct from outside the class with value() as reference? Thanks in advance!

回答1:

If understand you corretly, you want a pointer to a member function of myClass. You can achieve this by declaring the member function pointer as:

double (myClass::*value)(double,double) 

This function can later be called on an instance as:

(instance.*value)(x,y);

Alternatively you can use std::bind to create a function object which can be called as an ordinary function without having to keep track of the instance on which it is called after the call to std::bind:

auto& value = std::bind(myClass::value, instance);
// ....
value(x,y);


回答2:

Ok so far I found two solutions:

1) (General solution) Using an abstract base class which has a static pointer to the current instance and a static function that calls a function of the derived class. The static function can be used with a function pointer.

Example:

struct gsl_monte{
   double (*f)(double y);
};

class myBase {
private:
  static myBase* instance;

public:
  myBase(){};
  static void setInstance(myBase* newOne);
  virtual double value(double x) =0;
  static double callValue(double x);//{return value(x);}
};

class myClass : public myBase {
   public:
      myClass(){};
      double value(double x) {  return x;  };
};

myBase* myBase::instance = new myClass();
double  myBase::callValue(double x){return instance->value(x);}
void myBase::setInstance(myBase* newOne){instance=newOne;};

double g(double xx) {return xx;};

int main(int argc, char** argv ){
   double x[2]; x[0]=1.3; x[1]=1.3;
   myClass* instance = new myClass();
   myBase::setInstance(instance);
   instance->value(3);
   std::cout << "Test " << myBase::callValue(5) << std::endl;
   gsl_monte T;
   T.f=&myBase::callValue;
   double (*f)(double y, void*) = &myBase::callValue;
}

2) (Solution specific to my problem) Fortunatly the desired function accepts a parameter pointer, which I can use to pass the current object:

#include <iostream>
#include <functional>
using namespace std::placeholders;

struct gsl_monte{
   double (*f)(double y, void*);
};

class myClass {
   public:
      myClass(){};
      double value(double x) {  return x;  };
};

double valueTT(double x, void* param) {  return static_cast<myClass*>(param)->value(x);  };

int main(int argc, char** argv ){
   double x[2]; x[0]=1.3; x[1]=1.3;
   myClass* instance = new myClass();
   instance->value(3);
   gsl_monte T;
   T.f=&valueTT;
   double (*f)(double y, void*) = &valueTT;
}