Frage

Ich möchte ein std :: vector einige Funktionen enthalten, und dass mehr Funktionen in Echtzeit hinzugefügt werden kann. Alle Funktionen werden einen Prototyp wie folgt aussehen:

void Name (SDL_Event * event);

Ich weiß, wie eine Reihe von Funktionen zu machen, aber wie mache ich einen std :: vector von Funktionen? Ich habe dies versucht:

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

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

std::vector<void> functions;

std::vector<void*> functions;

Aber keiner von ihnen arbeitete. Bitte helfen

War es hilfreich?

Lösung

Versuchen Sie, eine typedef:

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

Andere Tipps

Versuchen Sie folgendes:

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

Wenn Sie erhöhen möchten dann dann Sie es wie folgt tun könnte:

#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;
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top