Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 230
Mattia Avatar asked Jul 26 '17 15:07

Mattia


People also ask

Why can't the this pointer be used in non member functions?

The this pointer points to the instance data for the object. The interrupt hardware/firmware in the system is not capable of providing the this pointer argument. You must use “normal” functions (non class members) or static member functions as interrupt service routines.

Which pointer is present inside every non static member function?

The 'this' pointer is passed as a hidden argument to all nonstatic member function calls and is available as a local variable within the body of all nonstatic functions. 'this' pointer is not available in static member functions as static member functions can be called without any object (with class name).

Can a static function call a non static function in C?

A static method provides NO reference to an instance of its class (it is a class method) hence, no, you cannot call a non-static method inside a static one.

How do you use pointers to member functions?

Using a pointer-to-member-function to call a function Calling the member function on an object using a pointer-to-member-function result = (object. *pointer_name)(arguments); or calling with a pointer to the object result = (object_ptr->*pointer_name)(arguments);


1 Answers

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.

like image 199
Vasiliy Galkin Avatar answered Sep 25 '22 04:09

Vasiliy Galkin