我正在尝试存储一个函数以便稍后调用,这是一个片段。
这很好用:
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”:对重载函数的调用不明确
感谢任何帮助。
最佳答案
指向静态成员函数的指针的类型看起来像指向非成员函数的指针:
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);
}
关于c++ - std::bind 类内部的静态成员函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21401552/