Skip to content

Instantly share code, notes, and snippets.

@r2p2
Created September 25, 2012 20:42
Show Gist options
  • Select an option

  • Save r2p2/3784305 to your computer and use it in GitHub Desktop.

Select an option

Save r2p2/3784305 to your computer and use it in GitHub Desktop.
#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());
}
@afett

afett commented Sep 29, 2012

Copy link
Copy Markdown

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.

@afett

afett commented Sep 29, 2012

Copy link
Copy Markdown

github ate my template parameters :-( second try
...
einmal in base class ihandle&lt:event_on> und einmal in ihandle&lt:event_off>
...

@afett

afett commented Sep 29, 2012

Copy link
Copy Markdown

grrr ....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment