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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Passing pointers to function into another function

Passing pointers to function into another function

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
12 Posts 6 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.
  • N Nemanja Trifunovic

    acerunner316 wrote:

    What am I doing wrong?

    From a quick glance at your code: you are trying to pass a pointer to a member-function where a pointer to a function is expected


    Programming Blog utf8-cpp

    A Offline
    A Offline
    acerunner316
    wrote on last edited by
    #3

    so i have to specify in the argument list of 'ManageTasks' to expect a member function and not a regular function? How do I do that?

    1 Reply Last reply
    0
    • A acerunner316

      I'm trying to pass a pointer to a function as an argument to another function, but I am getting compile errors.

      class CModule : public CDialog
      {
      // Construction
      public:
      CModule (CWnd* pParent = NULL); // standard constructor
      ...
      private:
      unsigned char FunctTask1(unsigned char, unsigned char);
      unsigned char FunctTask2(unsigned char, unsigned char);
      unsigned char FunctTask3(unsigned char, unsigned char);
      void ManageTasks(unsigned char, unsigned char, unsigned char (*)(unsigned char, unsigned char));
      };

      unsigned char CModule ::FunctTask1(unsigned char Param1, unsigned char Param2)
      {
      ...
      }
      unsigned char CModule ::FunctTask1(unsigned char Param1, unsigned char Param2)
      {
      ...
      }
      unsigned char CModule ::FunctTask1(unsigned char Param1, unsigned char Param2)
      {
      ...
      }

      void CModule ::ManageTasks(unsigned char Param1, unsigned char Param2, unsigned char (*ptrFunct)(unsigned char, unsigned char)
      {
      ... //unrelated code removed

      switch (ptrFunction(1,1))
      {
      case 0:
      //handle results
      case 1:
      //handle results
      }

      ... //unrelated code removed
      }

      now based on a combobox selection, I will call a different function

      void CModule::OnSelchangeCombobox()
      {
      switch(m_Combobox->GetCurSel())
      {
      case 0: ManageTask(1,1,FunctTask1); break;
      case 1: ManageTask(1,1,FunctTask2); break;
      case 2: ManageTask(1,1,FunctTask3); break;
      }
      }

      but i get this error on compile 'ManageTasks' : cannot convert parameter 3 from 'unsigned char (unsigned char,unsigned char)' to 'unsigned char (__cdec l *)(unsigned char,unsigned char)' What am I doing wrong? FYI I'm using VC++ 6.0.

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #4

      The methods FunctTask1(), FunctTask2(), FunctTask3() either need to be static or not a member of CModule.


      "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

      A 1 Reply Last reply
      0
      • A acerunner316

        I'm trying to pass a pointer to a function as an argument to another function, but I am getting compile errors.

        class CModule : public CDialog
        {
        // Construction
        public:
        CModule (CWnd* pParent = NULL); // standard constructor
        ...
        private:
        unsigned char FunctTask1(unsigned char, unsigned char);
        unsigned char FunctTask2(unsigned char, unsigned char);
        unsigned char FunctTask3(unsigned char, unsigned char);
        void ManageTasks(unsigned char, unsigned char, unsigned char (*)(unsigned char, unsigned char));
        };

        unsigned char CModule ::FunctTask1(unsigned char Param1, unsigned char Param2)
        {
        ...
        }
        unsigned char CModule ::FunctTask1(unsigned char Param1, unsigned char Param2)
        {
        ...
        }
        unsigned char CModule ::FunctTask1(unsigned char Param1, unsigned char Param2)
        {
        ...
        }

        void CModule ::ManageTasks(unsigned char Param1, unsigned char Param2, unsigned char (*ptrFunct)(unsigned char, unsigned char)
        {
        ... //unrelated code removed

        switch (ptrFunction(1,1))
        {
        case 0:
        //handle results
        case 1:
        //handle results
        }

        ... //unrelated code removed
        }

        now based on a combobox selection, I will call a different function

        void CModule::OnSelchangeCombobox()
        {
        switch(m_Combobox->GetCurSel())
        {
        case 0: ManageTask(1,1,FunctTask1); break;
        case 1: ManageTask(1,1,FunctTask2); break;
        case 2: ManageTask(1,1,FunctTask3); break;
        }
        }

        but i get this error on compile 'ManageTasks' : cannot convert parameter 3 from 'unsigned char (unsigned char,unsigned char)' to 'unsigned char (__cdec l *)(unsigned char,unsigned char)' What am I doing wrong? FYI I'm using VC++ 6.0.

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #5

        In addition to DavidCrow's reply.... You can pass pointers to member functions but to use them you have to have an object of the class they are a member of, just like any class method call. Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        1 Reply Last reply
        0
        • D David Crow

          The methods FunctTask1(), FunctTask2(), FunctTask3() either need to be static or not a member of CModule.


          "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

          "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

          A Offline
          A Offline
          acerunner316
          wrote on last edited by
          #6

          In that case, I can't use it the way I had intended. I need FunctTask1,2,&3 to be able to access other member variables and functions that aren't static, so making it static wont work. Time to rethink. Thanks for the help.

          D 1 Reply Last reply
          0
          • A acerunner316

            I'm trying to pass a pointer to a function as an argument to another function, but I am getting compile errors.

            class CModule : public CDialog
            {
            // Construction
            public:
            CModule (CWnd* pParent = NULL); // standard constructor
            ...
            private:
            unsigned char FunctTask1(unsigned char, unsigned char);
            unsigned char FunctTask2(unsigned char, unsigned char);
            unsigned char FunctTask3(unsigned char, unsigned char);
            void ManageTasks(unsigned char, unsigned char, unsigned char (*)(unsigned char, unsigned char));
            };

            unsigned char CModule ::FunctTask1(unsigned char Param1, unsigned char Param2)
            {
            ...
            }
            unsigned char CModule ::FunctTask1(unsigned char Param1, unsigned char Param2)
            {
            ...
            }
            unsigned char CModule ::FunctTask1(unsigned char Param1, unsigned char Param2)
            {
            ...
            }

            void CModule ::ManageTasks(unsigned char Param1, unsigned char Param2, unsigned char (*ptrFunct)(unsigned char, unsigned char)
            {
            ... //unrelated code removed

            switch (ptrFunction(1,1))
            {
            case 0:
            //handle results
            case 1:
            //handle results
            }

            ... //unrelated code removed
            }

            now based on a combobox selection, I will call a different function

            void CModule::OnSelchangeCombobox()
            {
            switch(m_Combobox->GetCurSel())
            {
            case 0: ManageTask(1,1,FunctTask1); break;
            case 1: ManageTask(1,1,FunctTask2); break;
            case 2: ManageTask(1,1,FunctTask3); break;
            }
            }

            but i get this error on compile 'ManageTasks' : cannot convert parameter 3 from 'unsigned char (unsigned char,unsigned char)' to 'unsigned char (__cdec l *)(unsigned char,unsigned char)' What am I doing wrong? FYI I'm using VC++ 6.0.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #7
            typedef unsigned char (CModule::*FUNCPTR)(unsigned char,unsigned char);
            ManageTask(1,1,&CModule::FunctTask1);
            
            A 1 Reply Last reply
            0
            • L Lost User
              typedef unsigned char (CModule::*FUNCPTR)(unsigned char,unsigned char);
              ManageTask(1,1,&CModule::FunctTask1);
              
              A Offline
              A Offline
              acerunner316
              wrote on last edited by
              #8

              tried that, but when I call ptrFunct(0,0); from inside ManageTask, I get an error saying "term does not evaluate to a function". Edit: nevermind. just realized i have to call it like this: (this->*ptrFunct)(0,0); -- modified at 18:42 Thursday 1st November, 2007

              1 Reply Last reply
              0
              • A acerunner316

                In that case, I can't use it the way I had intended. I need FunctTask1,2,&3 to be able to access other member variables and functions that aren't static, so making it static wont work. Time to rethink. Thanks for the help.

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #9

                acerunner316 wrote:

                I need FunctTask1,2,&3 to be able to access other member variables and functions that aren't static, so making it static wont work.

                You're not out of luck just yet. Two plausible solutions still exist. One is to change the third parameter of ManageTasks() to include CModule. I don't have the syntax readily available, but it's not uncommon and can easily be searched. The other would be to create two versions of FunctTask1(), FunctTask2(), and FunctTask3(). One would be static and the other would not. That would look something like:

                BYTE CModule::FunctTask1(BYTE Param1, BYTE Param2)
                {
                // access CModule variables and functions
                }

                static BYTE CModule::FunctTask1(BYTE Param1, BYTE Param2, CModule *pThis )
                {
                // call the non-static version
                return pThis->FunctTask1(Param1, Param2);
                }

                void CModule::ManageTasks(BYTE Param1, BYTE Param2, BYTE (*ptrFunct)(BYTE, BYTE), CModule *pThis )
                {
                ... //unrelated code removed
                switch (ptrFunction(1, 1, pThis))
                {
                case 0: // handle results
                case 1: // handle results
                }

                ... //unrelated code removed
                

                }

                void CModule::OnSelchangeCombobox()
                {
                switch(m_Combobox->GetCurSel())
                {
                case 0: ManageTask(1, 1, FunctTask1, this); break;
                case 1: ManageTask(1, 1, FunctTask2, this); break;
                case 2: ManageTask(1, 1, FunctTask3, this); break;
                }
                }


                "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

                "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                A 1 Reply Last reply
                0
                • D David Crow

                  acerunner316 wrote:

                  I need FunctTask1,2,&3 to be able to access other member variables and functions that aren't static, so making it static wont work.

                  You're not out of luck just yet. Two plausible solutions still exist. One is to change the third parameter of ManageTasks() to include CModule. I don't have the syntax readily available, but it's not uncommon and can easily be searched. The other would be to create two versions of FunctTask1(), FunctTask2(), and FunctTask3(). One would be static and the other would not. That would look something like:

                  BYTE CModule::FunctTask1(BYTE Param1, BYTE Param2)
                  {
                  // access CModule variables and functions
                  }

                  static BYTE CModule::FunctTask1(BYTE Param1, BYTE Param2, CModule *pThis )
                  {
                  // call the non-static version
                  return pThis->FunctTask1(Param1, Param2);
                  }

                  void CModule::ManageTasks(BYTE Param1, BYTE Param2, BYTE (*ptrFunct)(BYTE, BYTE), CModule *pThis )
                  {
                  ... //unrelated code removed
                  switch (ptrFunction(1, 1, pThis))
                  {
                  case 0: // handle results
                  case 1: // handle results
                  }

                  ... //unrelated code removed
                  

                  }

                  void CModule::OnSelchangeCombobox()
                  {
                  switch(m_Combobox->GetCurSel())
                  {
                  case 0: ManageTask(1, 1, FunctTask1, this); break;
                  case 1: ManageTask(1, 1, FunctTask2, this); break;
                  case 2: ManageTask(1, 1, FunctTask3, this); break;
                  }
                  }


                  "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

                  "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                  A Offline
                  A Offline
                  acerunner316
                  wrote on last edited by
                  #10

                  DavidCrow wrote:

                  You're not out of luck just yet. Two plausible solutions still exist. One is to change the third parameter of ManageTasks() to include CModule. I don't have the syntax readily available, but it's not uncommon and can easily be searched.

                  Tried that, see by reply to Randor below. I will try the overloaded function way. However, one of the reason I wrote my code the way it is was so that I can easily add more functions later (ie FunctTask4,5,6 etc) without having to change much code. Just add a new case in the OnSelchangeCombobox function. I can still call ManageTask to do the same things. Having to overload every single one seems inefficient to me.

                  D 1 Reply Last reply
                  0
                  • A acerunner316

                    I'm trying to pass a pointer to a function as an argument to another function, but I am getting compile errors.

                    class CModule : public CDialog
                    {
                    // Construction
                    public:
                    CModule (CWnd* pParent = NULL); // standard constructor
                    ...
                    private:
                    unsigned char FunctTask1(unsigned char, unsigned char);
                    unsigned char FunctTask2(unsigned char, unsigned char);
                    unsigned char FunctTask3(unsigned char, unsigned char);
                    void ManageTasks(unsigned char, unsigned char, unsigned char (*)(unsigned char, unsigned char));
                    };

                    unsigned char CModule ::FunctTask1(unsigned char Param1, unsigned char Param2)
                    {
                    ...
                    }
                    unsigned char CModule ::FunctTask1(unsigned char Param1, unsigned char Param2)
                    {
                    ...
                    }
                    unsigned char CModule ::FunctTask1(unsigned char Param1, unsigned char Param2)
                    {
                    ...
                    }

                    void CModule ::ManageTasks(unsigned char Param1, unsigned char Param2, unsigned char (*ptrFunct)(unsigned char, unsigned char)
                    {
                    ... //unrelated code removed

                    switch (ptrFunction(1,1))
                    {
                    case 0:
                    //handle results
                    case 1:
                    //handle results
                    }

                    ... //unrelated code removed
                    }

                    now based on a combobox selection, I will call a different function

                    void CModule::OnSelchangeCombobox()
                    {
                    switch(m_Combobox->GetCurSel())
                    {
                    case 0: ManageTask(1,1,FunctTask1); break;
                    case 1: ManageTask(1,1,FunctTask2); break;
                    case 2: ManageTask(1,1,FunctTask3); break;
                    }
                    }

                    but i get this error on compile 'ManageTasks' : cannot convert parameter 3 from 'unsigned char (unsigned char,unsigned char)' to 'unsigned char (__cdec l *)(unsigned char,unsigned char)' What am I doing wrong? FYI I'm using VC++ 6.0.

                    S Offline
                    S Offline
                    Stephen Hewitt
                    wrote on last edited by
                    #11

                    Modify the class as follows:

                    CModule : public CDialog
                    {
                    // Construction
                    public:
                       CModule (CWnd* pParent = NULL); // standard constructor
                    ...
                    private:
                       unsigned char FunctTask1(unsigned char, unsigned char);
                       unsigned char FunctTask2(unsigned char, unsigned char);
                       unsigned char FunctTask3(unsigned char, unsigned char);
                       typedef unsigned char (CModule::*PFunctTask_t)(unsigned char, unsigned char);
                       void ManageTasks(unsigned char, unsigned char, PFunctTask_t);
                    };

                    void CModule ::ManageTasks(unsigned char Param1, unsigned char Param2, unsigned char (*ptrFunct)(unsigned char, unsigned char)
                    {
                       ... //unrelated code removed

                    switch ((this->*ptrFunction)(1,1))
                       {
                       case 0:
                          //handle results
                       case 1:
                          //handle results
                       }

                    ... //unrelated code removed
                    }

                    And modify the calls like this:

                    void CModule::OnSelchangeCombobox()
                    {
                       switch(m_Combobox->GetCurSel())
                       {
                       case 0: ManageTask(1, 1, &CModule::FunctTask1); break;
                       case 1: ManageTask(1, 1, &CModule::FunctTask2); break;
                       case 2: ManageTask(1, 1, &CModule::FunctTask3); break;
                       }
                    }

                    -- modified at 20:49 Thursday 1st November, 2007

                    Steve

                    1 Reply Last reply
                    0
                    • A acerunner316

                      DavidCrow wrote:

                      You're not out of luck just yet. Two plausible solutions still exist. One is to change the third parameter of ManageTasks() to include CModule. I don't have the syntax readily available, but it's not uncommon and can easily be searched.

                      Tried that, see by reply to Randor below. I will try the overloaded function way. However, one of the reason I wrote my code the way it is was so that I can easily add more functions later (ie FunctTask4,5,6 etc) without having to change much code. Just add a new case in the OnSelchangeCombobox function. I can still call ManageTask to do the same things. Having to overload every single one seems inefficient to me.

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #12

                      acerunner316 wrote:

                      Having to overload every single one seems inefficient to me.

                      Indeed, I was just offering all the possibilities I could think of, and let you figure out which one would be best for your situation.


                      "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

                      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                      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