c++ pointer to non-static member functions

余生颓废 提交于 2021-02-07 02:57:51

问题


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.


回答1:


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.




回答2:


Your problem is that pointer-to-function is not the same as pointer-to-member-function. Read this for more info and this for clean way to write pointer-to-member-functions. If you need to keep this structure, make aaa a static or non-member function that takes everything it needs as arguments, or change the first argument of aaa to take a double(Funcs::*)(double) rather than a double(*fun)(double). You can even overload aaa to support both uses.




回答3:


Actually Funcs::Fun1 is not double(*)(double).

Any non-static method has this signature: return_type(*)(class_type* this, arguments...)

Lets look at exact types:

//first argument of `aaa` has type double(*)(double)
double aaa(double(*fun)(double), double x0);

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

  return f1;
}

// Fun1 has type double(Funs::*)(double)
// i.e it's a method of Funs and it takes (implicitly) first argument
// which is `this` pointer

// so that how `aaa` must  look like
double aaa(double(Funs::*fun)(double), double x0)
{
    // special sytax
    *this.*fun(x0);
}

// and that how `solver` looks like
double Funcs::solver(double X0)
{
  double result;

  // get pointer to method
  result = aaa(&Funs::Fun1, X0);
  return result;
}

If you are not familar with pointers to methods - check this




回答4:


I am still new to c++, but the solution I know is to have a static function acting as a delegate. https://docs.microsoft.com/ru-ru/windows/win32/learnwin32/managing-application-state-

///FOLLOW THE LINK FOR FULL CODE to understand what i am talking about :)

Take a look at how win32 API provides a static callback function acting as a delegate. in short, function takes number of inputs, and the last parameter is the class the caller passed as a void pointer.

static LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        DERIVED_TYPE *pThis = NULL;

        if (uMsg == WM_NCCREATE)
        {
            // routine to recast back pointer to your class type so you have access to your local members with "This"
            CREATESTRUCT* pCreate = (CREATESTRUCT*)lParam;  
            pThis = (DERIVED_TYPE*)pCreate->lpCreateParams;
            SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)pThis);

            pThis->m_hwnd = hwnd;
        }

Take a look at how lParam is being cast back to DERIVED_TYPE which is a type of your class and pThis acts as normal this and gives you the access to class members.



来源:https://stackoverflow.com/questions/45331989/c-pointer-to-non-static-member-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!