Pregunta

Quiero un std :: vector que contiene algunas funciones, y que más funciones se pueden añadir a ello en tiempo real. Todas las funciones tendrán un prototipo de esta manera:

void nombre (* SDL_Event evento);

Yo sé cómo hacer una gran variedad de funciones, pero ¿cómo puedo hacer un std :: vector de funciones? He intentado esto:

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

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

std::vector<void> functions;

std::vector<void*> functions;

Sin embargo, ninguno de ellos funcionó. Por favor, ayuda

¿Fue útil?

Solución

Trate de usar un typedef:

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

Otros consejos

Prueba esto:

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

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;
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top