Skip to content

Instantly share code, notes, and snippets.

@whoo24
Created May 6, 2013 09:26
Show Gist options
  • Save whoo24/5524192 to your computer and use it in GitHub Desktop.
Save whoo24/5524192 to your computer and use it in GitHub Desktop.
테이블에 함수를 포인터에 넣어놓고 문자열을 이용해 호출하는 코드
#include <map>
#include <string>
#include <functional>
#include <iostream>
using namespace std;
template <class T>
class Delegator
{
public:
map<string, function<void(T*)> > table;
void registerFunc( string cmd, void(T::* f)() )
{
table[cmd] = f;
}
void call(string cmd)
{
table[cmd]((T*)this);
}
};
class player : public Delegator<player>
{
public:
void play()
{
cout << "play" << endl;
}
virtual void play2nd()
{
cout << "player::play2nd" << endl;
}
};
class subplayer : public player
{
public:
virtual void play2nd()
{
cout << "subplayer::play2nd" << endl;
}
};
int main()
{
subplayer theplayer;
theplayer.registerFunc("play", &player::play);
theplayer.registerFunc("play2nd", &player::play2nd);
theplayer.call("play");
theplayer.call("play2nd");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment