2013-05-03 51 views
0

註冊回調我有回調的原型爲:C++從服務器

typedef void (*update)(int id,...); 
typedef void (*destroy)(int id,...); 
typedef void (*create)(int id, update* _update, destroy* _destroy); 

而不是創建回調函數:

void updateCB(int id,...){/*Add id to collection*/} 
void destroyCB(int id,...){/*Remove id from collection*/} 
void createCB(int id,update* _update, destroy* _destroy) 
{ 
    //Register Callbacks 
    *_update = updateCB; 
    *_destroy = destroyCB; 
} 

當我註冊回調編譯器給我的錯誤:

error: cannot convert 'ClassName::updateCB' from type 'void (ClassName::)(int,...)' to type 'update {aka void (*)(int..)}'

我怎樣纔能有效的註冊回調?

+0

函數指針與成員函數指針不相同。閱讀['std :: function'](http://en.cppreference.com/w/cpp/utility/functional/function)和['std :: bind'](http://en.cppreference.com/W/CPP /實用程序/功能/綁定)。 – 2013-05-03 10:29:23

+1

關於成員的指針。 – Aneri 2013-05-03 10:29:57

回答

2

您正在嘗試使用這種情況下不可能的成員函數。發生這種情況的原因是每個成員函數(如果它不是靜態的)都帶有隱藏指針,該指針指向這個的類實例。 您需要使用全局函數或靜態成員函數。