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