2009-07-11 25 views
12

Quiero que std :: vector contenga algunas funciones, y que se le puedan agregar más funciones en tiempo real. Todas las funciones tendrán un prototipo como este:std :: vector de funciones

nombre de vacío (evento SDL_Event *);

Sé cómo hacer una serie de funciones, pero ¿cómo hago un estándar :: de funciones? Intenté esto:

std::vector<(*)(SDL_Event *)> functions; 

std::vector<(*f)(SDL_Event *)> functions; 

std::vector<void> functions; 

std::vector<void*> functions; 

Pero ninguno de ellos funcionó. Por favor ayuda

Respuesta

15

intentar usar un typedef:

typedef void (*SDLEventFunction)(SDL_Event *); 
std::vector<SDLEventFunction> functions; 
+0

gracias, esta funcionó perfectamente! –

8

Prueba esto:

std::vector<void (*)(SDL_Event *)> functions; 
+0

Esto probablemente también hubiera funcionado –

1

Si te gusta impulsar a continuación, entonces usted podría hacerlo de esta manera:

#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; 
}