Created
November 22, 2019 20:17
-
-
Save krvajal/b6b33b35bab89434151adc236e9d6e95 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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