I'm quite a newbie using C ++. I am trying to implement in C ++, the state machine described in this link for C language: https://barrgroup.com/Embedded-Systems/How-To/Coding-State-Machines.
I have created an Fsm class, which implements the behavior of the state machine, and a Machine class, whose methods will be the states.
The code works, but it generates a warning (-Wpmf-conversions) when I try to save the memory address of a Machine method, in State state__
class Fsm
{
public:
typedef void (*State)();
private:
State state__;
public:
Fsm(State state);
void dispatch();
};
Fsm::Fsm(State state)
{
state__ = state ;
} //Fsm
void Fsm::dispatch()
{
(*state__)() ;
}
class Machine : public Fsm
{
public:
Machine() : Fsm((State)&Machine::initial) {} // ctor
};
I'm using an Atmega2560, with AVR-GCC compiler.
typedef void (*State)();
is a typedef for a function pointer to a function with no parameters and avoid
return type. It is perfectly normal to have multiple public, protected or private sections in a class.