指向非静态成员函数的C ++指针 [英] c++ pointer to non-static member functions

查看:54
本文介绍了指向非静态成员函数的C ++指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了很多有关非静态成员函数指针的文章和答案,但是似乎都无法解决我的问题.
因此,我创建了一个简短的示例来在这里复制我的问题:即使可以用不同的方式解决"该示例,对于最终软件,保持示例中的结构也很重要,谢谢.

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.

这是"Funcs.h"类的标题:

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);
};

这是"Funcs.cpp"类的cpp:

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);
}

最后这是主要的"main.cpp":

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;
}

当我调用"result = aaa(Fun1,X0);"时,错误出在Funcs :: solver方法中.因为我不能使用指向Fun1的指针,因为它是一个非静态成员.同时,我不能将其设为静态,否则在静态方法中将看不到变量"a".

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.

预先感谢您的帮助.

推荐答案

问题是您试图传递指向成员函数的指针,而预期指向非成员函数或静态成员函数的指针.这些是不同的类型.

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.

"fun"是指向函数的指针: double(* fun)(double).

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

这是指向Funcs类的成员函数的指针: double(Funcs :: * 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);
}

请参见实时示例在Coliru .

如果您要故意将方法 aaa 限制为仅将 Funcs 成员函数接受为 fun ,则这可能是一个不错的方法.如果您还想传递非成员函数,例如lambdas转换为 aaa ,请考虑改用 std :: function .

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.

这篇关于指向非静态成员函数的C ++指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆