Skip to content

Instantly share code, notes, and snippets.

@krvajal
Created November 22, 2019 20:17
Show Gist options
  • Save krvajal/b6b33b35bab89434151adc236e9d6e95 to your computer and use it in GitHub Desktop.
Save krvajal/b6b33b35bab89434151adc236e9d6e95 to your computer and use it in GitHub Desktop.
#include <cstdio>
class Command
{
public:
virtual void execute() = 0;
virtual ~Command();
protected:
Command();
};
Command::Command() {
}
Command::~Command() {
}
template <class Receiver>
class SimpleCommand : public Command
{
public:
typedef void (Receiver:: *Action)();
SimpleCommand(Receiver *r, Action a) : _receiver(r), _action(a) {}
void execute()
{
(_receiver->*_action)();
}
~SimpleCommand()
{
}
private:
Receiver *_receiver;
Action _action;
};
class Host
{
public:
void welcomeGuest()
{
printf("Hello guest\n");
}
};
int main()
{
Host h;
SimpleCommand<Host> c(&h, &Host::welcomeGuest);
c.execute();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment