Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Pointer to member function in struct

Pointer to member function in struct

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dean Seo
    wrote on last edited by
    #1

    Hi, Since I knew The Code Project, it has been helpful a lot. Now I think I have a small misunderstanding about pointer to member function in a struct and I need your help, which will be priceless to me. I am simply trying to see how pointer to member function works and it seems a little tricky. The code I am having a problem with is below.

    #include #include #define KEY_ESC 27

    using std::cout;
    using std::endl;

    class CAccount;

    struct StateMap
    {
    int input;
    void (CAccount::*mfp)();
    };

    class CAccount
    {
    public:
    enum
    {
    STATE_IDLE,
    STATE_INPUT,
    STATE_WAIT_ACK
    };

    public:
    StateMap map[3];
    int m_iState;

    CAccount();
    
    void OnIdle();
    void OnInput();
    void OnWaitAck();
    

    };//class CAccount

    CAccount::CAccount(){
    map[0].input = 1;
    map[0].mfp = &CAccount::OnIdle;

    map\[1\].input = 2;
    map\[1\].mfp = &CAccount::OnInput;
    
    map\[2\].input = 3;
    map\[2\].mfp = &CAccount::OnWaitAck;
    
    m\_iState = STATE\_IDLE;
    

    }

    void CAccount::OnIdle()
    {
    cout << "OnIdle()" << endl;
    }

    void CAccount::OnInput()
    {
    cout << "OnInput()" << endl;
    }

    void CAccount::OnWaitAck()
    {
    cout << "OnWaitAck()" << endl;
    }

    int main()
    {
    int ch = 0;
    int i;
    CAccount account;

    while ( ch != KEY\_ESC )
    {
    	ch = \_getch();
    	i  = 0;
    	while ( account.map\[i\].input != 0 )
    	{
    		if ( ch == account.map\[i\].input )
    		{
    			(account.(map\[i\].\*mfp))(); // compile error
    		}//if
    		i++;
    	}//while
    }//while
    
    return 0;
    

    }//main()

    I just don't know the right syntax for that part. How should I change that error line to make it work? Thanks in advance.

    E 1 Reply Last reply
    0
    • D Dean Seo

      Hi, Since I knew The Code Project, it has been helpful a lot. Now I think I have a small misunderstanding about pointer to member function in a struct and I need your help, which will be priceless to me. I am simply trying to see how pointer to member function works and it seems a little tricky. The code I am having a problem with is below.

      #include #include #define KEY_ESC 27

      using std::cout;
      using std::endl;

      class CAccount;

      struct StateMap
      {
      int input;
      void (CAccount::*mfp)();
      };

      class CAccount
      {
      public:
      enum
      {
      STATE_IDLE,
      STATE_INPUT,
      STATE_WAIT_ACK
      };

      public:
      StateMap map[3];
      int m_iState;

      CAccount();
      
      void OnIdle();
      void OnInput();
      void OnWaitAck();
      

      };//class CAccount

      CAccount::CAccount(){
      map[0].input = 1;
      map[0].mfp = &CAccount::OnIdle;

      map\[1\].input = 2;
      map\[1\].mfp = &CAccount::OnInput;
      
      map\[2\].input = 3;
      map\[2\].mfp = &CAccount::OnWaitAck;
      
      m\_iState = STATE\_IDLE;
      

      }

      void CAccount::OnIdle()
      {
      cout << "OnIdle()" << endl;
      }

      void CAccount::OnInput()
      {
      cout << "OnInput()" << endl;
      }

      void CAccount::OnWaitAck()
      {
      cout << "OnWaitAck()" << endl;
      }

      int main()
      {
      int ch = 0;
      int i;
      CAccount account;

      while ( ch != KEY\_ESC )
      {
      	ch = \_getch();
      	i  = 0;
      	while ( account.map\[i\].input != 0 )
      	{
      		if ( ch == account.map\[i\].input )
      		{
      			(account.(map\[i\].\*mfp))(); // compile error
      		}//if
      		i++;
      	}//while
      }//while
      
      return 0;
      

      }//main()

      I just don't know the right syntax for that part. How should I change that error line to make it work? Thanks in advance.

      E Offline
      E Offline
      Eugen Podsypalnikov
      wrote on last edited by
      #2

      Try the following :) :

      //...
      typedef void (*LPSTATEPROC) (void);

      struct StateMap
      {
      int input;
      LPSTATEPROC mfp;
      };
      //...

      They sought it with thimbles, they sought it with care; They pursued it with forks and hope; They threatened its life with a railway-share; They charmed it with smiles and soap. :)

      D 2 Replies Last reply
      0
      • E Eugen Podsypalnikov

        Try the following :) :

        //...
        typedef void (*LPSTATEPROC) (void);

        struct StateMap
        {
        int input;
        LPSTATEPROC mfp;
        };
        //...

        They sought it with thimbles, they sought it with care; They pursued it with forks and hope; They threatened its life with a railway-share; They charmed it with smiles and soap. :)

        D Offline
        D Offline
        Dean Seo
        wrote on last edited by
        #3

        Thanks.

        Eugen Podsypalnikov wrote:

        typedef void (*LPSTATEPROC) (void);

        You meant this, right?

        typedef void (CAccount::*LPSTATEPROC) (void);

        But it seems like it doesn't still work and the error occurs at the same line.

        #include
        #include

        #define KEY_ESC 27

        using std::cout;
        using std::endl;

        class CAccount;

        typedef void (CAccount::*LPSTATEPROC) (void);

        struct StateMap
        {
        int input;
        LPSTATEPROC mfp;
        };
        class CAccount
        {
        public:
        enum
        {
        STATE_IDLE,
        STATE_INPUT,
        STATE_WAIT_ACK
        };

        public:
        StateMap map[3];
        int m_iState;

        CAccount();
        
        void OnIdle();
        void OnInput();
        void OnWaitAck();
        

        };//class CAccount

        CAccount::CAccount(){
        map[0].input = 1;
        map[0].mfp = &CAccount::OnIdle;

        map\[1\].input = 2;
        map\[1\].mfp = &CAccount::OnInput;
        
        map\[2\].input = 3;
        map\[2\].mfp = &CAccount::OnWaitAck;
        
        m\_iState = STATE\_IDLE;
        

        }

        void CAccount::OnIdle()
        {
        cout << "OnIdle()" << endl;
        }

        void CAccount::OnInput()
        {
        cout << "OnInput()" << endl;
        }

        void CAccount::OnWaitAck()
        {
        cout << "OnWaitAck()" << endl;
        }

        int main()
        {
        int ch = 0;
        int i;
        CAccount account;

        while ( ch != KEY\_ESC )
        {
        	ch = \_getch();
        	i  = 0;
        	while ( account.map\[i\].input != 0 )
        	{
        		if ( ch == account.map\[i\].input )
        		{
        			(account.(map\[i\].\*mfp))(); // compile error
        		}//if
        		i++;
        	}//while
        }//while
        
        return 0;
        

        }//main()

        How can I change that error line to make it work? Thanks!

        L 1 Reply Last reply
        0
        • E Eugen Podsypalnikov

          Try the following :) :

          //...
          typedef void (*LPSTATEPROC) (void);

          struct StateMap
          {
          int input;
          LPSTATEPROC mfp;
          };
          //...

          They sought it with thimbles, they sought it with care; They pursued it with forks and hope; They threatened its life with a railway-share; They charmed it with smiles and soap. :)

          D Offline
          D Offline
          Dean Seo
          wrote on last edited by
          #4

          Hi, I just figured it out myself. But your reply helped me a lot to figure out to make it work! Thank you so much.

          1 Reply Last reply
          0
          • D Dean Seo

            Thanks.

            Eugen Podsypalnikov wrote:

            typedef void (*LPSTATEPROC) (void);

            You meant this, right?

            typedef void (CAccount::*LPSTATEPROC) (void);

            But it seems like it doesn't still work and the error occurs at the same line.

            #include
            #include

            #define KEY_ESC 27

            using std::cout;
            using std::endl;

            class CAccount;

            typedef void (CAccount::*LPSTATEPROC) (void);

            struct StateMap
            {
            int input;
            LPSTATEPROC mfp;
            };
            class CAccount
            {
            public:
            enum
            {
            STATE_IDLE,
            STATE_INPUT,
            STATE_WAIT_ACK
            };

            public:
            StateMap map[3];
            int m_iState;

            CAccount();
            
            void OnIdle();
            void OnInput();
            void OnWaitAck();
            

            };//class CAccount

            CAccount::CAccount(){
            map[0].input = 1;
            map[0].mfp = &CAccount::OnIdle;

            map\[1\].input = 2;
            map\[1\].mfp = &CAccount::OnInput;
            
            map\[2\].input = 3;
            map\[2\].mfp = &CAccount::OnWaitAck;
            
            m\_iState = STATE\_IDLE;
            

            }

            void CAccount::OnIdle()
            {
            cout << "OnIdle()" << endl;
            }

            void CAccount::OnInput()
            {
            cout << "OnInput()" << endl;
            }

            void CAccount::OnWaitAck()
            {
            cout << "OnWaitAck()" << endl;
            }

            int main()
            {
            int ch = 0;
            int i;
            CAccount account;

            while ( ch != KEY\_ESC )
            {
            	ch = \_getch();
            	i  = 0;
            	while ( account.map\[i\].input != 0 )
            	{
            		if ( ch == account.map\[i\].input )
            		{
            			(account.(map\[i\].\*mfp))(); // compile error
            		}//if
            		i++;
            	}//while
            }//while
            
            return 0;
            

            }//main()

            How can I change that error line to make it work? Thanks!

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            I think the line:

            			(account.(map\[i\].\*mfp))(); // compile error
            

            should not use indirection, and should read:

            			(account.(map\[i\].mfp))();
            

            Unrequited desire is character building. OriginalGriff

            C 1 Reply Last reply
            0
            • L Lost User

              I think the line:

              			(account.(map\[i\].\*mfp))(); // compile error
              

              should not use indirection, and should read:

              			(account.(map\[i\].mfp))();
              

              Unrequited desire is character building. OriginalGriff

              C Offline
              C Offline
              Chang Su Lee
              wrote on last edited by
              #6

              it is error too!

              L 1 Reply Last reply
              0
              • C Chang Su Lee

                it is error too!

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                What error?

                Unrequited desire is character building. OriginalGriff

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups