的std ::的函数矢量
题
我想要一个std ::矢量包含一些功能,而且更多的功能可以被添加到它实时。所有的功能将有一个原型是这样的:
空隙名称(SDL_Event *事件);
我知道如何使函数数组,但如何使功能一个std ::矢量?我已经试过这样:
std::vector<( *)( SDL_Event *)> functions;
std::vector<( *f)( SDL_Event *)> functions;
std::vector<void> functions;
std::vector<void*> functions;
但没有一次成功。请帮助
解决方案
尝试使用一个typedef:
typedef void (*SDLEventFunction)(SDL_Event *);
std::vector<SDLEventFunction> functions;
其他提示
尝试这种情况:
std::vector<void ( *)( SDL_Event *)> functions;
如果你喜欢刺激,然后那么你可以做这样的:
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <vector>
void f1(SDL_Event *event)
{
// ...
}
void f2(SDL_Event *event)
{
// ...
}
int main()
{
std::vector<boost::function<void(SDL_Event*)> > functions;
functions.push_back(boost::bind(&f1, _1));
functions.push_back(boost::bind(&f2, _1));
// invoke like this:
SDL_Event * event1 = 0; // you should probably use
// something better than 0 though..
functions[0](event1);
return 0;
}
不隶属于 StackOverflow