2014-01-28 38 views
6

我想存储一个函数以后再调用,这里是一个片段。std ::在类内部绑定一个静态成员函数

这工作得很好:

void RandomClass::aFunc(int param1, int param2, double param3, bool isQueued /*= false */) 
{ 
    /* If some condition happened, store this func for later */ 
    auto storeFunc = std::bind (&RandomClass::aFunc, this, param1, param2, param3, true); 

    CommandList.push(storeFunc); 

    /* Do random stuff */ 
} 

但是,如果RandomClass是静态的,所以我相信我应该这样做:

void RandomClass::aFunc(int param1, int param2, double param3, bool isQueued /*= false */) 
{ 
    /* If some condition happened, store this func for later */ 
    auto storeFunc = std::bind (&RandomClass::aFunc, param1, param2, param3, true); 

    CommandList.push(storeFunc); 

    /* Do random stuff */ 
} 

但是,这并不工作,我得到的编译错误

错误C2668:'std :: tr1 :: bind':对超载函数的模糊调用

任何帮助赞赏。

+2

是否有多于一个的过载' RandomClass :: aFunc'? – juanchopanza

+0

另外我可以做到这一点,它适用于普通类,但不适用于静态版本。 \t CommandList.push([=]() \t { \t \t aFunc(参数1,参数2,参数3,TRUE); \t}); –

+0

是的,还有第二个:: aFunc具有不同的参数。另一个函数使用字符串,与我试图使用的参数相比,它的参数更少。 –

回答

14

类型的指针的静态成员函数的看起来像一个指针指向一个非成员fuinction:

auto storeFunc = std::bind ((void(*)(WORD, WORD, double, bool)) 
           &CSoundRouteHandlerApp::MakeRoute, 
           sourcePort, destPort, volume, true); 

下面是一个简化的例子:

struct Foo 
{ 
    void foo_nonstatic(int, int) {} 
    static int foo_static(int, int, int) { return 42;} 
}; 

#include <functional> 
int main() 
{ 
    auto f_nonstatic = std::bind((void(Foo::*)(int, int))&Foo::foo_nonstatic, Foo(), 1, 2); 
    auto f_static = std::bind((int(*)(int, int, int))&Foo::foo_static, 1, 2, 3); 

} 
+0

这正是需要的,非常感谢! –

+0

我不能upvote你或者出于某种原因将其标记为答案。如果有人可以,请将其标记为正确。 –

+0

@StaetonGrey我认为你可以将它标记为答案,但你需要更多的代表点才能投票。 – juanchopanza