2014-02-08 80 views
1

大家好:)我有一個問題與函數指針
我「回調」函數的參數是:
1)這樣的函數:int(* FX)(INT, INT)
2)int變量:整數一個
3)另一INT:整數b
好了,問題是,我想傳遞給「回調」函數是一個非靜態函數成員:(和有是很多問題
如果有人比我聰明有一些時間花費,他可以看我的代碼:)C++傳遞指向非靜態成員函數

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

有一種方法來創建我的指針,我試圖寫,就像int(* fx)(int,int)= something?
我搜索了很多,但沒有人可以給我一個答案(當然,有一個回答:「NO」,但我仍然認爲我可以做一些事情)

我也聽說過仿函數,可以幫助他們我在這種情況下?

由於任何人
PS:抱歉,我的英語不好

EDIT1: 我可以使用這樣的事情:

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

,並在我的主:

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 

我並不完全感到難過,因爲我不想讓我的'callback2'的第一個參數(對象)...
對於不理解我的(壞)解釋的人來說,問題是:我可以刪除callback2函數中的第一個參數嗎?
原型將是

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

,我會叫這樣的:

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

什麼是「這樣」?爲什麼用'c'標記? – 2014-02-08 23:37:52

+0

@ H2CO3 - 你會和我一樣糟糕! –

+0

@EdHeal:P(我的目標不是要成爲壞人,而是老實說......你明白這個人究竟在問什麼,或者他甚至想要做什麼?因爲坦率地說,我不知道。我也不明白爲什麼當人們在編譯器錯誤到谷歌錯誤信息時做的第一件事情。我只是不明白爲什麼。) – 2014-02-08 23:41:01

回答

0

功能不能被引用這樣:

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

您必須通過功能的地址是這樣的:

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

表達function2衰變成指針到功能,其允許它的工作。

+1

我不能使它工作:(從int(A :: *)無效轉換爲int(*) – incud

+0

好吧,它的工作原理:)我只是不得不分散修改一些東西,但現在工作。謝謝 – incud

0

我將與性病函子做,這裏是一個基於關你的代碼一個簡單的例子:

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

這段代碼沒什麼問題,但我想修改'回調'函數。不可能僅通過「回調」函數來引用我的obj函數? – incud

相關問題