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. MFC's message map, no need of &?

MFC's message map, no need of &?

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorialquestion
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

    It's syntax that we have to put '&' right before pointer to member function. For example here.

    class Test;
    typedef void (Test::*fpop)();
    class Test
    {
    public:
    void Op1(){}
    };

    int main(){
    fpop pFunc;
    pFunc = &Test::Op1; // we must need &

    return 0;
    

    }

    However, when I take a look at ON_COMMAND(or any other messages) in MFC, it seems a bit different from what I think is right. VS6.0 is okay. It follows the right syntax as you see below. You can clearly see & before memberFxn.

    #define ON_COMMAND(id, memberFxn) \
    { WM_COMMAND, CN_COMMAND, (WORD)id, (WORD)id, AfxSig_vv, (AFX_PMSG)&memberFxn },
    // ON_COMMAND(id, OnFoo) is the same as
    // ON_CONTROL(0, id, OnFoo) or ON_BN_CLICKED(0, id, OnFoo)

    But in VS2008, it goes a bit weird. There is no & before memberFxn.

    #define ON_COMMAND(id, memberFxn) \
    { WM_COMMAND, CN_COMMAND, (WORD)id, (WORD)id, AfxSigCmd_v, \
    static_cast (memberFxn) },
    // ON_COMMAND(id, OnBar) is the same as
    // ON_CONTROL(0, id, OnBar) or ON_BN_CLICKED(0, id, OnBar)

    Moreover, in spite of the fact that there is no & before memberFxn, each line below works perfectly. 1. ON_COMMAND(ID_APP_ABOUT, CSingleApp::OnAppAbout) // & 2. ON_COMMAND(ID_APP_ABOUT, &CSingleApp::OnAppAbout) // no & I tried to find why, and I was curious if it could be because of static_cast<> but it turned out that static_cast has nothing to do with it. So I am wondering why in VS2008 I have 2 choices where I put & or I don't have to put &.

    P 1 Reply Last reply
    0
    • D Dean Seo

      It's syntax that we have to put '&' right before pointer to member function. For example here.

      class Test;
      typedef void (Test::*fpop)();
      class Test
      {
      public:
      void Op1(){}
      };

      int main(){
      fpop pFunc;
      pFunc = &Test::Op1; // we must need &

      return 0;
      

      }

      However, when I take a look at ON_COMMAND(or any other messages) in MFC, it seems a bit different from what I think is right. VS6.0 is okay. It follows the right syntax as you see below. You can clearly see & before memberFxn.

      #define ON_COMMAND(id, memberFxn) \
      { WM_COMMAND, CN_COMMAND, (WORD)id, (WORD)id, AfxSig_vv, (AFX_PMSG)&memberFxn },
      // ON_COMMAND(id, OnFoo) is the same as
      // ON_CONTROL(0, id, OnFoo) or ON_BN_CLICKED(0, id, OnFoo)

      But in VS2008, it goes a bit weird. There is no & before memberFxn.

      #define ON_COMMAND(id, memberFxn) \
      { WM_COMMAND, CN_COMMAND, (WORD)id, (WORD)id, AfxSigCmd_v, \
      static_cast (memberFxn) },
      // ON_COMMAND(id, OnBar) is the same as
      // ON_CONTROL(0, id, OnBar) or ON_BN_CLICKED(0, id, OnBar)

      Moreover, in spite of the fact that there is no & before memberFxn, each line below works perfectly. 1. ON_COMMAND(ID_APP_ABOUT, CSingleApp::OnAppAbout) // & 2. ON_COMMAND(ID_APP_ABOUT, &CSingleApp::OnAppAbout) // no & I tried to find why, and I was curious if it could be because of static_cast<> but it turned out that static_cast has nothing to do with it. So I am wondering why in VS2008 I have 2 choices where I put & or I don't have to put &.

      P Offline
      P Offline
      Peter_in_2780
      wrote on last edited by
      #2

      It goes waaaay back to the original C language of the 1970's. In the first edition of "The C Programming Language",

      Kernighan and Ritchie wrote:

      ... an identifier which is declared "function returning ...", when used except in the function-name position of a call, is converted to "pointer to function returning ...".

      In the second edition, the wording has changed to

      ... and expression of type "function returning T," except when used as the operand of the & operator, is converted to "pointer to function returning T."

      In other words, the & has always been optional. Cheers, Peter

      Software rusts. Simon Stephenson, ca 1994.

      D 1 Reply Last reply
      0
      • P Peter_in_2780

        It goes waaaay back to the original C language of the 1970's. In the first edition of "The C Programming Language",

        Kernighan and Ritchie wrote:

        ... an identifier which is declared "function returning ...", when used except in the function-name position of a call, is converted to "pointer to function returning ...".

        In the second edition, the wording has changed to

        ... and expression of type "function returning T," except when used as the operand of the & operator, is converted to "pointer to function returning T."

        In other words, the & has always been optional. Cheers, Peter

        Software rusts. Simon Stephenson, ca 1994.

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

        First of all, Thanks you so much for answering my question. However, I think that case that you mentioned is about just "pointer to function."

        Peter_in_2780 wrote:

        the & has always been optional.

        But when using "pointer to MEMBER function", the & is not optional, but necessary as you see below.

        class Test;
        typedef void (Test::*fpop)();
        class Test
        {
        public:
        void Op1(){}
        };

        int main(){
        fpop pFunc;
        pFunc = Test::Op1; // compile error!
        //pFunc = &Test::Op1; // the & is essential.
        return 0;
        }

        I know that MFC's message pump consists of Pointer to member function, which needs the &. Could you explain why? Thanks.

        P 1 Reply Last reply
        0
        • D Dean Seo

          First of all, Thanks you so much for answering my question. However, I think that case that you mentioned is about just "pointer to function."

          Peter_in_2780 wrote:

          the & has always been optional.

          But when using "pointer to MEMBER function", the & is not optional, but necessary as you see below.

          class Test;
          typedef void (Test::*fpop)();
          class Test
          {
          public:
          void Op1(){}
          };

          int main(){
          fpop pFunc;
          pFunc = Test::Op1; // compile error!
          //pFunc = &Test::Op1; // the & is essential.
          return 0;
          }

          I know that MFC's message pump consists of Pointer to member function, which needs the &. Could you explain why? Thanks.

          P Offline
          P Offline
          Peter_in_2780
          wrote on last edited by
          #4

          Digging a bit deeper, I came across this[^] right here on CP. Seems VC6 and VC7 break the rules as you noticed... See just after the second code block below the heading "Member Function Pointers". Way too much information in that article for me! But if I ever need to go there... Cheers, Peter

          Software rusts. Simon Stephenson, ca 1994.

          A 1 Reply Last reply
          0
          • P Peter_in_2780

            Digging a bit deeper, I came across this[^] right here on CP. Seems VC6 and VC7 break the rules as you noticed... See just after the second code block below the heading "Member Function Pointers". Way too much information in that article for me! But if I ever need to go there... Cheers, Peter

            Software rusts. Simon Stephenson, ca 1994.

            A Offline
            A Offline
            Albert Holguin
            wrote on last edited by
            #5

            That looks like a great article... Looks like I have some reading ahead of me... :)

            U 1 Reply Last reply
            0
            • A Albert Holguin

              That looks like a great article... Looks like I have some reading ahead of me... :)

              U Offline
              U Offline
              User 7793637
              wrote on last edited by
              #6

              It's a simple problem. At First, the massage map is implemented by an array with a structure. Secondly, the syntax error is actually a kind of warning. Third and finally, The massage map function starts with disabling the warning by a macro PTM_WARNING_DISABLE So, you may assign the function to a pointer without reference operator &. Here is an example, PTM_WARNING_DISABLE fpop pFunc[1] = {Test::Op1}; PTM_WARNING_RESTORE However, an interest problem occurs when you call the function with the function pointer. And the problem is all yours. :-)

              modified on Wednesday, August 31, 2011 5:42 AM

              A 1 Reply Last reply
              0
              • U User 7793637

                It's a simple problem. At First, the massage map is implemented by an array with a structure. Secondly, the syntax error is actually a kind of warning. Third and finally, The massage map function starts with disabling the warning by a macro PTM_WARNING_DISABLE So, you may assign the function to a pointer without reference operator &. Here is an example, PTM_WARNING_DISABLE fpop pFunc[1] = {Test::Op1}; PTM_WARNING_RESTORE However, an interest problem occurs when you call the function with the function pointer. And the problem is all yours. :-)

                modified on Wednesday, August 31, 2011 5:42 AM

                A Offline
                A Offline
                Albert Holguin
                wrote on last edited by
                #7

                did you mean to send this message to the OP? ...because I didn't ask the question...

                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