Thread: Array of member functions

  1. #1
    Registered User Ward's Avatar
    Join Date
    Sep 2001
    Location
    Belgium
    Posts
    39

    Unhappy Array of member functions

    Hi,
    I want to implement a statemachine using an array of member function pointers instead of using switch-case statements:

    This is the original situation:
    Code:
    class MyClass
    {
       private:
        UCHAR m_ucState;
       public:
        BOOL HandleState();
        BOOL HandleState_1();
        BOOL HandleState_2();
        BOOL HandleState_3();
    }
    
    BOOL MyClass::HandleState()
    {
       switch(m_ucState)
       {
          case 1:
             return HandleState_1();
          case 2:
             return HandleState_2();
          case 3:
             return HandleState_3();
       }
    }
    I want to change the HandleState function to something like this:
    Code:
    BOOL MyClass::HandleState()
    {
       return m_myStateHandlers[m_ucState]();
    }
    Can anyone help me how I should declare the 'm_myStateHandlers' array? I should be a membervariable and all HandleState_... functions are not static.

    Thanks in advance,

    Ward
    Greetings.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Can anyone help me how I should declare the 'm_myStateHandlers' array
    You declare any array by specifying its size and the type of things you will store in the the array. What does the type 'pointer to member function' look like? See this current thread:

    http://cboard.cprogramming.com/showt...77504&p=548572
    Last edited by 7stud; 04-04-2006 at 01:58 AM.

  3. #3
    Registered User Ward's Avatar
    Join Date
    Sep 2001
    Location
    Belgium
    Posts
    39

    Declaration of m_myStateHandlers

    BOOL (*m_myStateHandlers[3])(void);

    This was not the problem, but I can not fill it up.
    Greetings.

  4. #4
    #define WORLD "sad place" LinuxCoder's Avatar
    Join Date
    Mar 2006
    Location
    Portugal
    Posts
    89
    Code:
    BOOL (*m_myStateHandlers[3])(void);
    Shouldn't this be:
    Code:
    static BOOL (MyClass::*m_myStateHandlers[3])(void);
    I guess it must be a pointer to a member function instead of a pointer to a function, also i believe you can use it as static since the methods are not supposed to move and that way you'll only have to initialize them once. About the initialization part i'm still trying to figure it out...

    I can be sooooo wrong though, cheers

  5. #5
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Quote Originally Posted by Ward
    BOOL (*m_myStateHandlers[3])(void);

    This was not the problem, but I can not fill it up.
    Code:
    class MyClass
    {
       private:
        UCHAR m_ucState;
        BOOL (*m_myStateHandlers[3])(void);
       public:
        MyClass();
        BOOL HandleState();
        BOOL HandleState_1();
        BOOL HandleState_2();
        BOOL HandleState_3();
    };
    
    MyClass::MyClass()
    {
       int i = 0;
       m_myStateHandlers[i++] = &HandleState_1;
       m_myStateHandlers[i++] = &HandleState_2;
       m_myStateHandlers[i++] = &HandleState_3;
    }
    
    BOOL MyClass::HandleState()
    {
       return (*m_myStateHandlers[m_ucState])();
    }
    Last edited by homeyg; 04-04-2006 at 08:55 PM.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Rather than an index such as m_ucState and a separate array of function pointers, how about using a single pointer to member function to contain the state information? Something along this line:
    Code:
    #include <iostream>
    
    class FSM
    {
       void (FSM::*pmf)(); // pointer to member function for current state
       void init()   { std::cout << "init"   << '\n'; pmf = &FSM::idle;   }
       void idle()   { std::cout << "idle"   << '\n'; pmf = &FSM::active; }
       void active() { std::cout << "active" << '\n'; pmf = &FSM::idle;   }
    public:
       FSM(void (FSM::*pmf_)() = &FSM::init ) : pmf(pmf_) {}
       void run()
       {
          (this->*pmf)();
       }
    };
    
    int main() 
    {
       FSM fsm;
       for ( int i = 0; i < 5; ++i )
       {
          fsm.run();
       }
       return 0;
    }
    
    /* my output
    init
    idle
    active
    idle
    active
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User Ward's Avatar
    Join Date
    Sep 2001
    Location
    Belgium
    Posts
    39

    Response to the latest posts

    Answer to 'homeyg':
    I've tried something similar like you posted last. But I got compiler errors. I will verify my code against yours and test it again.
    Thanks you.

    Answer to 'Dave_Sinkula':
    This is an interesting alternative way of working! At first sight is seems very difficult to perform debugging on it (keeping the overview of the states in sight), but I will surely test this out.
    Thanks.

    Kind regards,

    Ward

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Some debuggers might show the current function name, which would tell you the state.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM