C++传递非静态成员函数指针

C++传递非静态成员函数指针,c++,function,pointers,non-static,C++,Function,Pointers,Non Static,大家好:我对函数指针有问题 我的“回调”函数参数是: 1像这样的函数:int*fxint,int 2一个int变量:int a 3另一个int:int b 好吧,问题是我想传递给“callback”的函数是一个非静态函数成员:而且有很多问题 如果比我聪明的人有时间,他可以查看我的代码: #include <iostream> using namespace std; class A{ private: int x; public: A(int elem){

大家好:我对函数指针有问题 我的“回调”函数参数是: 1像这样的函数:int*fxint,int 2一个int变量:int a 3另一个int:int b 好吧,问题是我想传递给“callback”的函数是一个非静态函数成员:而且有很多问题 如果比我聪明的人有时间,他可以查看我的代码:

#include <iostream>
using namespace std;

class A{
private:
    int x;
public:
    A(int elem){
        x = elem;
    }

    static int add(int a, int b){
        return a + b;
    }

    int sub(int a, int b){
        return x - (a + b);
    }
};

void callback( int(*fx)(int, int), int a, int b)
{
    cout << "Value of the callback: " << fx(a, b) << endl;
}

int main()
{
A obj(5);

    //PASSING A POINTER TO A STATIC MEMBER FUNCTION -- WORKS!!
    // output = 'Value of the callback: 30'
    callback(A::add, 10, 20);

    //USING A POINTER TO A NON-STATIC MEMBER FUNCTION -- WORKS!!
    int(A::*function1)(int, int) = &A::sub;
    // output = 'Non static member: 3'
    cout << "Non static member: " << (obj.*function1)(1, 1) << endl;

    //PASSING A POINTER TO A NON-STATIC MEMBER FUNCTION -- aargh
    // fallita! tutto quello sotto non funziona --> usa i funtori???
    // puoi creare una classe wrapper ma non riuscirai mai a chiamare da callback
    int(A::*function2)(int, int) = &A::sub;
    int(*function3)(int, int) = obj.*function2; //[error] invalid use of non-static member function
    callback(function3, 1, 1);
}
我还没有完全被区分,因为我不想将我的'callback2'第一个参数传递给对象。。。 对于那些不理解我糟糕解释的人来说,问题是:我可以删除callback2函数中的第一个参数吗? 原型将是

void callback2(int(*fx)(int, int), int a, int b)<br>

不能以这种方式引用函数:

您必须按如下方式传递函数的地址:


expression function 2退化为一个指向函数的指针,该指针允许它工作。

我将使用std functor来完成此操作,下面是一个基于您的代码的简单示例:

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

class A{
private:
    int x;
public:
    A(int elem){
        x = elem;
    }

    static int add(int a, int b){
        return a + b;
    }

    int sub(int a, int b) const{
        return x - (a + b);
    }
};

void callback( std::function<int(const A& ,int,int )> fx, A obj, int a, int b)
{
    cout << "Value of the callback: " << fx( obj, a, b) << endl;
}

int main()
{
A obj(5);


    std::function<int(const A& ,int,int )> Aprinter= &A::sub;

    callback(Aprinter,obj,1,2);
}

这边是什么?为什么这上面贴着c?@H2CO3-你会和我一样坏的@埃德海勒:P我的目标不是做坏事,但老实说。。。你明白这家伙在问什么或者他想做什么吗?因为坦白说,我不知道。我也不明白为什么当人们在谷歌上搜索错误信息时,获取编译器错误不是第一件事。我只是不明白为什么不。你是对的,对不起,你可能想相信我,但我谷歌了很多。唯一的答案是:“你不能”或“写一个包装函数”。我正在寻找另一种解决方案这段代码还可以,但我必须修改“callback”函数。不可能只传递引用我的obj的函数“callback”?我无法使它工作:从intA::*到int*的转换无效。好的,它工作了:我只是需要分散地修改一些东西,但现在可以工作了。谢谢
void callback2(int(*fx)(int, int), int a, int b)<br>
callback2(&obj.sub, 1, 3);
int (*function3)(int, int) = obj.*function2;
int (*function3)(int, int) = std::mem_fn(&A::sub, obj);
//                           ^^^^^^^^^^^^^^^^^^^^^^^^^
#include <iostream>
#include <functional>
using namespace std;

class A{
private:
    int x;
public:
    A(int elem){
        x = elem;
    }

    static int add(int a, int b){
        return a + b;
    }

    int sub(int a, int b) const{
        return x - (a + b);
    }
};

void callback( std::function<int(const A& ,int,int )> fx, A obj, int a, int b)
{
    cout << "Value of the callback: " << fx( obj, a, b) << endl;
}

int main()
{
A obj(5);


    std::function<int(const A& ,int,int )> Aprinter= &A::sub;

    callback(Aprinter,obj,1,2);
}