Search code examples
c++function-pointersnon-static

c++ pointer to non-static member functions


I have read many posts and answers about pointers to non-static member functions, but none looks able to solve my problem.
So I have created a short example to replicate my issue here: even if this example could be "solved" in different ways, for the final software it is important to keep the structure like in the example, thanks.

This is the header of the class "Funcs.h":

class Funcs
{
private:
  double a = 1, b = 2;

public:
  Funcs();
  ~Funcs();
  double Fun1(double X);
  double solver(double X0);
  double aaa(double(*fun)(double), double x0);
};

This is the cpp of the class "Funcs.cpp":

#include "Funcs.h"

using namespace std;

Funcs::Funcs()
{
}

Funcs::~Funcs()
{
}

double Funcs::Fun1(double X) {
  double f1 = a*X;

  return f1;
}

double Funcs::solver(double X0)
{
  double result;

  result = aaa(Fun1, X0);
  return result;
}

double Funcs::aaa(double(*fun)(double), double x0)
{
  return fun(x0);
}

And Finally this is the main "main.cpp":

#include <iostream>
#include "Funcs.h"

using namespace std;

int main() {
  double x0=1;
  double result;
  Funcs funcs;

  result = funcs.solver(x0);
  cout << result << endl;

  return 0;
}

The error is in the method Funcs::solver when I call "result = aaa(Fun1, X0);" because I can't use a pointer to Fun1 because it is a non-static member. At the same time I can't make it static, otherwise the variable "a" could not be seen inside the static method.

Thanks in advance for your help.


Solution

  • The problem is that you're trying to pass a pointer to a member function while a pointer to either a non-member function or a static member function is expected. And those are different types.

    Here "fun" is a pointer to function: double(*fun)(double).

    And here it's a pointer to a member function of class Funcs: double(Funcs::*fun)(double)

    So here's how you can modify your code to make it work.

    class Funcs
    {
      // all the rest is the same
      double aaa(double(Funcs::*fun)(double), double x0);
    };
    
    double Funcs::solver(double X0)
    {
      // ...
      result = aaa(&Funcs::Fun1, X0);
      // ...
    }
    
    double Funcs::aaa(double(Funcs::*fun)(double), double x0)
    {
      return (this->*fun)(x0);
    }
    

    See live example at Coliru.

    This may be a fine way to go if you want to deliberately limit your method aaa to accept only Funcs member functions as fun. If you'd also like to pass the non-member functions or e.g. lambdas to aaa, consider using std::function instead.