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. Syntax of How to Call a Member Function via a Function Pointer

Syntax of How to Call a Member Function via a Function Pointer

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++helptutorial
4 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.
  • B Offline
    B Offline
    Bram van Kampen
    wrote on last edited by
    #1

    Hi, I have a Base Class Header File as follows,

    // Header File:

    class MyClass,// Advance Declaration
    typedef void (MyClass::*PFN_DO_SOMETHING)(void* pData);

    class MyClass{

    // Lots of things

    PFN_DO_SOMETHING m_pfnDoSomething;
    void Act(void* pData);
    }

    //Implementation File:
    void MyClass::Act(void* pData){
    m_pfnDoSomething(pData);
    }

    Needless to say, 'pfnDoSomething' has been initialised elsewere. I boiled the question down to the essentials, the correct CPP syntax for calling a member function from a pointer to it. I get a Compiler error stating that 'pfnDoSomething' does not resolve to a function call. What is the correct syntax for calling this function. regards :)

    Bram van Kampen

    J L L 3 Replies Last reply
    0
    • B Bram van Kampen

      Hi, I have a Base Class Header File as follows,

      // Header File:

      class MyClass,// Advance Declaration
      typedef void (MyClass::*PFN_DO_SOMETHING)(void* pData);

      class MyClass{

      // Lots of things

      PFN_DO_SOMETHING m_pfnDoSomething;
      void Act(void* pData);
      }

      //Implementation File:
      void MyClass::Act(void* pData){
      m_pfnDoSomething(pData);
      }

      Needless to say, 'pfnDoSomething' has been initialised elsewere. I boiled the question down to the essentials, the correct CPP syntax for calling a member function from a pointer to it. I get a Compiler error stating that 'pfnDoSomething' does not resolve to a function call. What is the correct syntax for calling this function. regards :)

      Bram van Kampen

      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #2

      The solution:

      class MyClass{

      // Lots of things
      typedef void (\*MyClass::PFN\_DO\_SOMETHING)(void\* pData);
      PFN\_DO\_SOMETHING m\_pfnDoSomething;
      void Act(void\* pData);
      

      };

      //Implementation File:
      void MyClass::Act(void* pData){
      m_pfnDoSomething(pData);
      }

      Your errors:

      • The asterisk in the typedef is at the wrong place.
      • Place the typedef inside the class to avoid the forward declaration which would not work here with the correct typedef.
      • The function member name is m_pfnDoSomething (not pfnDoSomething).

      Note also that there is no need to use the class prefix with the typedef. You may also use:

      typedef void (*PFN_DO_SOMETHING)(void* pData);

      1 Reply Last reply
      0
      • B Bram van Kampen

        Hi, I have a Base Class Header File as follows,

        // Header File:

        class MyClass,// Advance Declaration
        typedef void (MyClass::*PFN_DO_SOMETHING)(void* pData);

        class MyClass{

        // Lots of things

        PFN_DO_SOMETHING m_pfnDoSomething;
        void Act(void* pData);
        }

        //Implementation File:
        void MyClass::Act(void* pData){
        m_pfnDoSomething(pData);
        }

        Needless to say, 'pfnDoSomething' has been initialised elsewere. I boiled the question down to the essentials, the correct CPP syntax for calling a member function from a pointer to it. I get a Compiler error stating that 'pfnDoSomething' does not resolve to a function call. What is the correct syntax for calling this function. regards :)

        Bram van Kampen

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

        I've always had difficulty with this, but if memory serves, function pointers in classes are required to be declared static. [edit] Memory does not serve. [/edit]

        1 Reply Last reply
        0
        • B Bram van Kampen

          Hi, I have a Base Class Header File as follows,

          // Header File:

          class MyClass,// Advance Declaration
          typedef void (MyClass::*PFN_DO_SOMETHING)(void* pData);

          class MyClass{

          // Lots of things

          PFN_DO_SOMETHING m_pfnDoSomething;
          void Act(void* pData);
          }

          //Implementation File:
          void MyClass::Act(void* pData){
          m_pfnDoSomething(pData);
          }

          Needless to say, 'pfnDoSomething' has been initialised elsewere. I boiled the question down to the essentials, the correct CPP syntax for calling a member function from a pointer to it. I get a Compiler error stating that 'pfnDoSomething' does not resolve to a function call. What is the correct syntax for calling this function. regards :)

          Bram van Kampen

          L Offline
          L Offline
          leon de boer
          wrote on last edited by
          #4

          As per Jochen's answer but I would also add a guard against your forgetting to set the pointer checking it's not NULL. The typecast and advance definition is also unnecessary unless you actually need the type for something, the pointer will be checked for match when you attempt to set it with or without the typecast.

          void MyClass::Act(void* pData){
          if (m_pfnDoSomething) m_pfnDoSomething(pData);
          }

          Here is a minimalist function ptr .. no need for forward delaration, typedef or even a name of field. All you need is the * dereference before the function ptr name it knows what you are doing from that and really all it needs is what the function looks like so when you try to set it, that it checks for match.

          class MyClass{
          void(*MyClass::m_pfnDoSomething)(void*);

          void MyClass::Act(void\* pData){
          	if (m\_pfnDoSomething) m\_pfnDoSomething(pData);
          }
          

          };

          Finally especially since your in a class I would suggest you don't pass in the void* data pointer but look at other options to typecast or marshal the data in a way that is a little safer.

          In vino veritas

          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