13

std::vector にいくつかの関数を含め、より多くの関数をリアルタイムで追加できるようにしたい。すべての関数には、次のようなプロトタイプがあります。

ボイド名(SDL_Event *イベント);

関数の配列を作成する方法は知っていますが、関数の std::vector を作成するにはどうすればよいですか? 私はこれを試しました:

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

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

std::vector<void> functions;

std::vector<void*> functions;

しかし、どれも機能しませんでした。助けてください

4

3 に答える 3

17

typedef を使用してみてください:

typedef void (*SDLEventFunction)(SDL_Event *);
std::vector<SDLEventFunction> functions;
于 2009-07-11T00:36:52.777 に答える
8

これを試して:

std::vector<void ( *)( SDL_Event *)> functions;
于 2009-07-11T00:39:29.703 に答える
1

ブーストが好きなら、次のようにすることができます:

#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;
}
于 2009-07-11T00:49:41.657 に答える