user56661: 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.

user4143: A pointer to a member function is not allowed in C++. GCC can allow it, but moans at you because it's non-standard.
In C++ the normal way of dealing with the kind of thing you want is through class inheritance.
Typically you would create your base Fsm class that implements the basic functionality of your state machine control, then overlay over the top of that (create a new class that inherits the Fsm class) another class that implements your specific machine functionality.  You can declare functions in your base Fsm class that are pure virtual - that is, they exist, but have no body. It's then the job of the child class to actually implement that functionality.  The parent Fsm class then knows about those functions and can call them.
So you would have something like:
class Fsm {
    public:
        void dispatch();
    protected:
        virtual void initial() = 0;
}; 

void Fsm::dispatch() {
    initial() ;
}



class Machine : public Fsm {
    protected:
        void initial() {
             Serial.println("Here");
        }
}; 

Machine mach;

mach.dispatch(); // --> "Here"

Fsm knows about initial(), but the function hasn't been further defined.  Machine implements initial().  You then create an instance of Machine which contains everything needed.  Calling dispatch() runs the function in the Fsm parent class which then calls initial() in the child Machine class.

56661: <p>I'm quite a newbie using C ++. I am trying to implement in C ++, the state machine described in this link for C language:
<a href="https://barrgroup.com/Embedded-Systems/How-To/State-Machines-Event-Driven-Systems" rel="nofollow noreferrer">https://barrgroup.com/Embedded-Systems/How-To/Coding-State-Machines</a>.</p>

<p>I have created an Fsm class, which implements the behavior of the state machine, and a Machine class, whose methods will be the states. </p>

<p>The code works, but it generates a warning (-Wpmf-conversions) when I try to save the memory address of a Machine method, in <code>State state__</code></p>

<pre><code>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)&amp;Machine::initial) {}       // ctor
}; 
</code></pre>

<p>I'm using an Atmega2560, with AVR-GCC compiler.</p>
 4143: <p>A pointer to a member function is not allowed in C++. GCC <em>can</em> allow it, but moans at you because it's non-standard.</p>

<p>In C++ the normal way of dealing with the kind of thing you want is through class inheritance.</p>

<p>Typically you would create your base <code>Fsm</code> class that implements the basic functionality of your state machine control, then overlay over the top of that (create a new class that inherits the <code>Fsm</code> class) another class that implements your specific machine functionality.  You can declare functions in your base <code>Fsm</code> class that are <em>pure virtual</em> - that is, they exist, but have no body. It's then the job of the child class to actually implement that functionality.  The parent <code>Fsm</code> class then knows about those functions and can call them.</p>

<p>So you would have something like:</p>

<pre><code>class Fsm {
    public:
        void dispatch();
    protected:
        virtual void initial() = 0;
}; 

void Fsm::dispatch() {
    initial() ;
}



class Machine : public Fsm {
    protected:
        void initial() {
             Serial.println("Here");
        }
}; 

Machine mach;

mach.dispatch(); // --&gt; "Here"
</code></pre>

<p><code>Fsm</code> knows about <code>initial()</code>, but the function hasn't been further defined.  <code>Machine</code> implements <code>initial()</code>.  You then create an instance of <code>Machine</code> which contains everything needed.  Calling <code>dispatch()</code> runs the function in the <code>Fsm</code> parent class which then calls <code>initial()</code> in the child <code>Machine</code> class.</p>
