C ++传递指向非静态成员函数的指针 [英] C++ Passing pointer to non-static member function

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

问题描述

大家好:)我有一个函数指针的问题

我的'回调'函数参数是:

1)这样的函数:int(* fx)(int ,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< 回调的值:< fx(a,b)< endl;
}

int main()
{
A obj(5);

//通过指向静态成员函数的指针 - 工作!
// output ='回调的值:30'
callback(A :: add,10,20);

//将指针用于非静态成员函数 - 工作!
int(A :: * function1)(int,int)=& A :: sub;
// output ='Non static member:3'
cout<< 非静态成员:< (obj。* function1)(1,1)< endl;

//通向非静态成员函数的指针 - aargh
// fallita! tutto quello sotto non funziona - >美国?
// 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]无效使用非静态成员函数
callback(function3,1,1);
}

有一种方法可以创建我的指针, int(* fx)(int,int)=什么?
我搜索了很多,但没有人可以给我一个答案(好,有一个答案:NO,但我仍然认为我可以做一些事情)



我也听说过职业演员,在这种情况下他们能帮助我吗?



感谢任何人

PS:对不起我的英语。



EDIT1:
我可以这样使用:



 模板< class T> 
void callback2(T * obj,int(T :: * fx)(int,int),int a,int b)
{
cout< 回调的值:< (obj→* fx)(a,b) endl;
}
void callback2(void * nullpointer,int(* fx)(int,int),int a,int b)
{
cout< 回调的值:< fx(a,b) endl;
}

并在我的主目录中:

  callback2(NULL,& mul,5,3); //泛型函数,就像:int mul(int a,int b){return a * b;} 
callback2(NULL,& A :: add,5,3) // static member function
callback2(& obj,& A :: sub,1,1); //非静态成员函数

我不是完全悲伤,因为我不想传递我的'callback2'的第一个参数(对象)...

这个问题,对于那些不明白我的(坏)解释,是我可以删除我的callback2函数的第一个参数?

原型将是

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

,我会这样调用:

  callback2(& obj.sub,1,3); 


解决方案

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


  int(* function3)(int,int)= obj。* function2; 


您必须传递函数的地址, / p>


  int(* function3)(int,int)= std :: mem_fn(& :sub,obj); 
// ^^^^^^^^^^^^^^^^^^^^^^^


表达式 function2 衰减为指针函数,允许它工作。 p>

Hi everyone :) I have a problem with function pointers
My 'callback' function arguments are:
1) a function like this: int(*fx)(int,int)
2) an int variable: int a
3) another int: int b
Well, the problem is that the function I want to pass to 'callback' is a non-static function member :( and there are lots of problems
If someone smarter than me have some time to spent, he can look my code :)

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

There's a way to create my pointer in the way I tried to wrote, like int(*fx)(int, int) = something?
I searched a lot but no-one could gave me an answer (well, there was an answer: "NO", but I still think I can do something)

I heard also about functors, may them help me in this case?

Thanks to anyone
PS: sorry for my bad english

EDIT1: I can use something like this:

template <class T>
void callback2( T* obj, int(T::*fx)(int, int), int a, int b)
{
    cout << "Value of the callback: " << (obj->*fx)(a, b) << endl;
}
void callback2( void* nullpointer, int(*fx)(int, int), int a, int b)
{
    cout << "Value of the callback: " << fx(a, b) << endl;
}

and in my main:

callback2(NULL, &mul, 5, 3); // generic function, it's like: int mul(int a, int b){return a*b;}
callback2(NULL, &A::add, 5, 3); //static member function
callback2(&obj, &A::sub, 1, 1); //non static member function

I'm not completely sadisfied, because I don't want to pass my 'callback2' the first parameter (the object)...
The question, for people that didn't understand my (bad) explanation, is: can I delete the first parameter in my callback2 function?
the prototype will be

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

and I will call like this:

callback2(&obj.sub, 1, 3);

解决方案

Functions cannot be referenced this way:

int (*function3)(int, int) = obj.*function2;

You have to pass the address of the function like this:

int (*function3)(int, int) = std::mem_fn(&A::sub, obj);
//                           ^^^^^^^^^^^^^^^^^^^^^^^^^

The expression function2 decays into a pointer-to-function which allows it to work.

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

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