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. A function pointer problem...

A function pointer problem...

Scheduled Pinned Locked Moved C / C++ / MFC
help
18 Posts 9 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.
  • C Cedric Moonen

    A function member of a class doesn't have the same prototype as a global function: there is the 'this' paramter that is passed as an implicit argument of the function (but it is transparent to the user). To avoid that, you can declare your member function as static. A static function is shared aming all instances of your class and thus doesn't require the implicit 'this' parameter. But of course, within a static function, you can only access static member of your class (because the function does not belong to a specific instance of the class).

    O Offline
    O Offline
    Owner drawn
    wrote on last edited by
    #5

    Good. Well said. Thanks a lot.:-D

    Jesus Loves:rose:

    --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:

    1 Reply Last reply
    0
    • O Owner drawn

      void AFunction(void(*)())
      {
      //i am innocent
      }
      //A class member function
      void CAClass::AFunc()
      {
      //I am getting an error...(it's given below)
      //is it wrong to do this
      //or can this be done in some other way.
      AFunction(this->AFunc);
      }

      error C2664: 'func' : cannot convert parameter 1 from 'void (void)' to 'void (__cdecl *)(void)'

      Jesus Loves:rose:

      --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:

      B Offline
      B Offline
      BadKarma
      wrote on last edited by
      #6

      Hi, You can alsoo change AFunction to

      void AFunction(void(CAClass::*pFunction)())
      {
      //i am still innocent
      }

      this way you can pass a pointer to a member function. This said you will need to have an object of the type CAClass available to execute the function. codito ergo sum

      O 1 Reply Last reply
      0
      • B BadKarma

        Hi, You can alsoo change AFunction to

        void AFunction(void(CAClass::*pFunction)())
        {
        //i am still innocent
        }

        this way you can pass a pointer to a member function. This said you will need to have an object of the type CAClass available to execute the function. codito ergo sum

        O Offline
        O Offline
        Owner drawn
        wrote on last edited by
        #7

        Hmm...Cool:cool::rose: Nice one.

        BadKarma wrote:

        //i am still innocent

        Now that's funny...He he:laugh:

        Jesus Loves:rose:

        --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:

        1 Reply Last reply
        0
        • O Owner drawn

          Yeah it works with static, but why not with member functions. Sounds silly but still would like to know. Since it is also a function. Edit:Thanks a lot prakash.

          Jesus Loves:rose:

          --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:

          P Offline
          P Offline
          Prakash Nadar
          wrote on last edited by
          #8

          Owner drawn wrote:

          Sounds silly but still would like to know. Since it is also a function.

          no its not silly, coz it has a good logic behind it, member function has a different "hidden" signature than other functions.


          -Prakash

          O 1 Reply Last reply
          0
          • P Prakash Nadar

            Owner drawn wrote:

            Sounds silly but still would like to know. Since it is also a function.

            no its not silly, coz it has a good logic behind it, member function has a different "hidden" signature than other functions.


            -Prakash

            O Offline
            O Offline
            Owner drawn
            wrote on last edited by
            #9

            Mr.Prakash wrote:

            no its not silly

            :)

            Mr.Prakash wrote:

            coz it has a good logic behind it, member function has a different "hidden" signature than other functions.

            Yea you are right.

            K 1 Reply Last reply
            0
            • O Owner drawn

              Mr.Prakash wrote:

              no its not silly

              :)

              Mr.Prakash wrote:

              coz it has a good logic behind it, member function has a different "hidden" signature than other functions.

              Yea you are right.

              K Offline
              K Offline
              Kedar Potdar
              wrote on last edited by
              #10

              [Message Deleted]

              P O 2 Replies Last reply
              0
              • K Kedar Potdar

                [Message Deleted]

                P Offline
                P Offline
                Prakash Nadar
                wrote on last edited by
                #11

                kdar wrote:

                The functions are mangled so that they can be uniquely identified and called upon invocation

                no you are wrong. mangling is nothing to do with it. mangaling is more relevant for compilers and linkers. The hidden param is the this param passed to the member functions.


                -Prakash

                1 Reply Last reply
                0
                • K Kedar Potdar

                  [Message Deleted]

                  O Offline
                  O Offline
                  Owner drawn
                  wrote on last edited by
                  #12

                  kdar wrote:

                  The functions are mangled so that they can be uniquely identified and called upon invocation

                  :doh:How. Can you please elaborate. It would be helpful. Edit:Where did he go:confused:Jesus Loves:rose:

                  --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose: -- modified at 6:47 Thursday 12th January, 2006

                  P T 2 Replies Last reply
                  0
                  • O Owner drawn

                    kdar wrote:

                    The functions are mangled so that they can be uniquely identified and called upon invocation

                    :doh:How. Can you please elaborate. It would be helpful. Edit:Where did he go:confused:Jesus Loves:rose:

                    --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose: -- modified at 6:47 Thursday 12th January, 2006

                    P Offline
                    P Offline
                    Prakash Nadar
                    wrote on last edited by
                    #13

                    Owner drawn wrote:

                    Edit:Where did he go

                    he left coz he could not support his theory.:rolleyes:


                    -Prakash

                    1 Reply Last reply
                    0
                    • O Owner drawn

                      kdar wrote:

                      The functions are mangled so that they can be uniquely identified and called upon invocation

                      :doh:How. Can you please elaborate. It would be helpful. Edit:Where did he go:confused:Jesus Loves:rose:

                      --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose: -- modified at 6:47 Thursday 12th January, 2006

                      T Offline
                      T Offline
                      toxcct
                      wrote on last edited by
                      #14

                      Owner drawn wrote:

                      Edit:Where did he go

                      urgent need to go to the toilets !? :-O


                      TOXCCT >>> GEII power
                      [toxcct][VisualCalc 2.20][VisualCalc 3.0]

                      O 1 Reply Last reply
                      0
                      • T toxcct

                        Owner drawn wrote:

                        Edit:Where did he go

                        urgent need to go to the toilets !? :-O


                        TOXCCT >>> GEII power
                        [toxcct][VisualCalc 2.20][VisualCalc 3.0]

                        O Offline
                        O Offline
                        Owner drawn
                        wrote on last edited by
                        #15

                        :zzz:

                        Jesus Loves:rose:

                        --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:

                        T 1 Reply Last reply
                        0
                        • O Owner drawn

                          void AFunction(void(*)())
                          {
                          //i am innocent
                          }
                          //A class member function
                          void CAClass::AFunc()
                          {
                          //I am getting an error...(it's given below)
                          //is it wrong to do this
                          //or can this be done in some other way.
                          AFunction(this->AFunc);
                          }

                          error C2664: 'func' : cannot convert parameter 1 from 'void (void)' to 'void (__cdecl *)(void)'

                          Jesus Loves:rose:

                          --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:

                          C Offline
                          C Offline
                          Chris Losinger
                          wrote on last edited by
                          #16

                          i use this macro:

                          // call mem fn that takes a single int
                          #define CALL_MEMBER_FN(object,ptrToMember) ((object).*(ptrToMember)(int))

                          // fns have a single int param
                          typedef void (CObjType::*fnType)(int);

                          ...

                          // holds a pointer to mem fn
                          fnType m_fnPtr;

                          ...

                          // call it
                          CALL_MEMBER_FN(*this, fnPtr)(5);

                          Cleek | Image Toolkits | Thumbnail maker -- modified at 11:44 Thursday 12th January, 2006

                          1 Reply Last reply
                          0
                          • C Cedric Moonen

                            A function member of a class doesn't have the same prototype as a global function: there is the 'this' paramter that is passed as an implicit argument of the function (but it is transparent to the user). To avoid that, you can declare your member function as static. A static function is shared aming all instances of your class and thus doesn't require the implicit 'this' parameter. But of course, within a static function, you can only access static member of your class (because the function does not belong to a specific instance of the class).

                            B Offline
                            B Offline
                            Blake Miller
                            wrote on last edited by
                            #17

                            within a static function, you can only access static member of your class (because the function does not belong to a specific instance of the class). Unless you also pass a pointer to the object into the functiona as an additional argument, then you can access public members and functions of the class using the pointer to the class. Marriage slows down your coding, a baby slows it down even more!

                            1 Reply Last reply
                            0
                            • O Owner drawn

                              :zzz:

                              Jesus Loves:rose:

                              --Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:

                              T Offline
                              T Offline
                              ThatsAlok
                              wrote on last edited by
                              #18

                              Owner drawn wrote:

                              ForumVisual C++ Subject:Re: Zzzz Sender:Owner drawn Date:6:56 12 Jan '06

                              don't sleep :)

                              "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                              cheers, Alok Gupta VC Forum Q&A :- I/ IV

                              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