문제

STD :: Vector가 일부 기능을 포함하기를 원하며 더 많은 기능을 실시간으로 추가 할 수 있습니다. 모든 기능에는 다음과 같은 프로토 타입이 있습니다.

void name (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;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top