Created
September 25, 2012 20:42
-
-
Save r2p2/3784305 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 <iostream> | |
| struct event_on {}; | |
| struct event_off {}; | |
| template<class TEvent> | |
| struct ihandle | |
| { | |
| virtual void handle(TEvent event) = 0; | |
| }; | |
| template<class TState> | |
| struct state_machine | |
| { | |
| template<class TEvent> | |
| void handle(const TEvent &event) | |
| { state->handle(event); } | |
| TState *state; | |
| }; | |
| struct state_light | |
| : public ihandle<event_on> | |
| , public ihandle<event_off> | |
| {}; | |
| struct state_light_on : public state_light | |
| { | |
| virtual void handle(event_on event) | |
| { std::cout << "light is already on" << std::endl; } | |
| virtual void handle(event_off event) | |
| { std::cout << "switch to off" << std::endl; } | |
| }; | |
| struct state_light_off : public state_light | |
| { | |
| virtual void handle(event_on event) | |
| { std::cout << "switch to on" << std::endl; } | |
| virtual void handle(event_off event) | |
| { std::cout << "light is already off" << std::endl; } | |
| }; | |
| int main() | |
| { | |
| state_light_on slon; | |
| state_light_off sloff; | |
| state_machine<state_light> sm; | |
| sm.state = &slon; | |
| sm.handle(event_on()); | |
| sm.handle(event_off()); | |
| sm.state = &sloff; | |
| sm.handle(event_on()); | |
| sm.handle(event_off()); | |
| } |
github ate my template parameters :-( second try
...
einmal in base class ihandle<:event_on> und einmal in ihandle<:event_off>
...
grrr ....
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Der Compiler sucht erst die function in dem Fall "handle" auf struct state_light. Jetzt erkennt er, dass es den zweimal gibt, einmal
in base class ihandle<event_on> und einmal in ihandle<event_off>. Da kommt er schon durcheinander. Der eigentliche overload würde später
passieren, aber soweit kommt er nicht.