我遇到了函數指針的問題,並且我在網絡上找到的任何東西都幫助我解決了這個問題。如何從成員函數中獲取「簡單」函數指針
我有選自C API,它需要一個void函數的指針的函數:
extern int APIFunction(int, void (*func)(int));
我有,我想,當我調用API函數把函數的類。
class MyClass
{
public:
void myFunction(int status, otherAPi arguments...);
};
然後,我創建了一個指向我的會員功能,創建了類的新實例
typedef void (MyClass::*MyClassFunctionPointer)(int stat, otherAPi arguments...);
MyClassFunctionPointer fctPointer= &MyClass::myFunction;
LicenseSecurity instance;
我得到一個錯誤,當我嘗試打電話給我的API函數與我創建的函數指針:
E2034 Impossible to convert 'void (MyClass::*)(int, otherAPITypes...)' into 'void (*) (int, otherAPITypes...)'
E2342 Bad type correspondence in the parameter 'func' ('void (*)(int, otherAPITypes...)' desired, 'void(int, otherAPITypes...)' obtained)
:
int stat = APIFunction(5, fctPointer); // -> error 1
int stat = APIFunction(5, instance.*fctPointer); // -> error 2
我在第一和第二種情況分別得到了錯誤
我無法訪問API函數,因此我無法修改它。爲了總結這個問題:如何獲得一個「簡單的」C函數指針來放入我的類的成員函數的函數參數?
謝謝
我認爲boost :: bind在這裏很有幫助 – Constantin 2013-04-11 20:49:33
@Constantin然後他會有一個包裝成員函數調用的函子......但他仍然無法將它傳遞給API函數。 – 2013-04-11 20:55:01
[C++類成員函數回調]可能的重複(http://stackoverflow.com/questions/8079453/c-class-member-function-callback) – 2013-04-11 20:55:16