Passing Member Functions as Function Pointers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cps

    #1

    Passing Member Functions as Function Pointers

    Hi,

    I'm a C programmer taking my first steps into the world of C++.

    I'm currently developing a C++ 3D graphics application using GLUT
    (OpenGL Utility Toolkit written in C) for the GUI components.

    The application is built around a "World" object that contains a "GUI"
    object that is a C++ wrapper around GLUT. What I'd like to do is pass a
    pointer to a member function in the World class to function in the GUI
    class.

    Here are the functions I'd like to pass:


    void World::Display( )
    {
    /* clear all pixels */
    glClear(GL_COLO R_BUFFER_BIT);

    Do OpenGL stuff...
    }

    void World::Keyboard (unsigned char key, int x, int y)
    {
    cout << key << endl;

    Handle keyboard...
    }


    I'd like to pass these functions to the constructor of the GUI object
    as follows:


    p_GUI = new GUI(Display, Keyboard);


    I'm getting the following error:

    World.cpp(16) : error C2664: 'GUI::GUI(void (__cdecl *)(void),void
    (__cdecl *)(unsigned char,int,int))' : cannot convert parameter 1 from
    'void (void)' to 'void (__cdecl *)(void)'
    None of the functions with this name in scope match the target
    type

    I understand that (__cdecl *) is a calling convention. What I can't
    figure out is how to fix the syntax so that the code compiles.

    Any help with this (especially a simple example) would be very much
    appreciated.

    Cheers,

    Chris

  • Jim Langston

    #2
    Re: Passing Member Functions as Function Pointers


    "cps" <cshare01@qub.a c.uk> wrote in message
    news:1142637654 .633138.68550@i 39g2000cwa.goog legroups.com...[color=blue]
    > Hi,
    >
    > I'm a C programmer taking my first steps into the world of C++.
    >
    > I'm currently developing a C++ 3D graphics application using GLUT
    > (OpenGL Utility Toolkit written in C) for the GUI components.
    >
    > The application is built around a "World" object that contains a "GUI"
    > object that is a C++ wrapper around GLUT. What I'd like to do is pass a
    > pointer to a member function in the World class to function in the GUI
    > class.
    >
    > Here are the functions I'd like to pass:
    >
    >
    > void World::Display( )
    > {
    > /* clear all pixels */
    > glClear(GL_COLO R_BUFFER_BIT);
    >
    > Do OpenGL stuff...
    > }
    >
    > void World::Keyboard (unsigned char key, int x, int y)
    > {
    > cout << key << endl;
    >
    > Handle keyboard...
    > }
    >
    >
    > I'd like to pass these functions to the constructor of the GUI object
    > as follows:
    >
    >
    > p_GUI = new GUI(Display, Keyboard);[/color]

    I don't see your definition of GUI::GUI anywhere. What are your constructor
    parms?
    [color=blue]
    > I'm getting the following error:
    >
    > World.cpp(16) : error C2664: 'GUI::GUI(void (__cdecl *)(void),void
    > (__cdecl *)(unsigned char,int,int))' : cannot convert parameter 1 from
    > 'void (void)' to 'void (__cdecl *)(void)'
    > None of the functions with this name in scope match the target
    > type
    >
    > I understand that (__cdecl *) is a calling convention. What I can't
    > figure out is how to fix the syntax so that the code compiles.
    >
    > Any help with this (especially a simple example) would be very much
    > appreciated.
    >
    > Cheers,
    >
    > Chris
    >[/color]


    Comment

    • cps

      #3
      Re: Passing Member Functions as Function Pointers

      Here they are:


      // GUI.h

      #ifndef GUI_H
      #define GUI_H

      class GUI
      {
      public:
      GUI();
      GUI(void (*displayFuncti onPtr)(), void
      (*keyboardFunct ionPtr)(unsigne d char key, int x, int y));
      ~GUI();

      private:
      };

      #endif


      GUI::GUI(void (*displayFuncti onPtr)(), void
      (*keyboardFunct ionPtr)(unsigne d char key, int x, int y))
      {
      int argc = 0;
      char* argv[30];

      glutInit(&argc, argv);
      glutInitDisplay Mode(GLUT_DOUBL E | GLUT_RGB);
      glutInitWindowS ize(250, 250);
      glutInitWindowP osition(100, 100);
      glutCreateWindo w("Test");
      glClearColor(0. 0, 0.0, 0.0, 0.0);
      glShadeModel(GL _FLAT);
      glMatrixMode(GL _PROJECTION);
      glLoadIdentity( );
      glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
      glutDisplayFunc (displayFunctio nPtr);
      glutIdleFunc(di splayFunctionPt r);
      glutKeyboardFun c(keyboardFunct ionPtr);
      glutMainLoop();
      }

      Comment

      • Paul

        #4
        Re: Passing Member Functions as Function Pointers

        "cps" <cshare01@qub.a c.uk> wrote in message
        news:1142637654 .633138.68550@i 39g2000cwa.goog legroups.com...[color=blue]
        > Hi,
        >
        > I'm a C programmer taking my first steps into the world of C++.
        >[/color]

        Please see the FAQ:


        - Paul



        Comment

        • BobR

          #5
          Re: Passing Member Functions as Function Pointers


          cps wrote in message ...[color=blue]
          >Here they are:
          >// GUI.h
          >#ifndef GUI_H
          >#define GUI_H
          >class GUI{ public:
          > GUI();
          > GUI(void (*displayFuncti onPtr)(), void
          > (*keyboardFunct ionPtr)(unsigne d char key, int x, int y));
          > ~GUI();
          > private:
          > };
          >#endif
          >
          >GUI::GUI(voi d (*displayFuncti onPtr)(), void
          > (*keyboardFunct ionPtr)(unsigne d char key, int x, int y)){
          > int argc = 0;
          > char* argv[30];
          > glutInit(&argc, argv);[/color]
          [snip][color=blue]
          > glutDisplayFunc (displayFunctio nPtr);
          > glutIdleFunc(di splayFunctionPt r);
          > glutKeyboardFun c(keyboardFunct ionPtr);
          > glutMainLoop();
          >}[/color]

          If Paul's suggestion doesn't help, try making the 'call-back' functions
          'static'. That worked for me in one Glut pgm I was playing with. See (google
          for) 'GlutMaster' [1]

          class World{
          public:
          static void Display();
          static void Keyboard(unsign ed char key, int x, int y);
          };
          void World::Display( ){
          glClear(GL_COLO R_BUFFER_BIT); // clear all pixels
          // Do OpenGL stuff...
          }
          void World::Keyboard (unsigned char key, int x, int y){
          cout << key << endl;
          // Handle keyboard...
          }

          [1] - might still be at URL: http://www.duke.edu/~stetten/GlutMaster/
          --
          Bob R
          POVrookie


          Comment

          • cpsmusic@yahoo.com

            #6
            Re: Passing Member Functions as Function Pointers

            Thanks for the replies.

            The link to the "pointers-to-members" page should do the trick.

            Also, I tried making the member functions static. This worked, but then
            I couldn't access member variables.

            Cheers,

            Chris

            Comment

            • cpsmusic@yahoo.com

              #7
              Re: Passing Member Functions as Function Pointers

              I'm still having problems with this.

              I've had a look at the "pointers-to-members" page. It says that
              [color=blue]
              >Normal C functions can be thought of as having a different calling convention from >member functions, so the types of their pointers (pointer-to-member-function vs. >pointer-to-function) are different and incompatible. C++ introduces a new type of >pointer, called a pointer-to-member, which can be invoked only by providing an object.[/color]

              Does this mean that the only way I can get this to work is by passing
              the object that contains the member functions? In this case, do I need
              to pass function pointers at all, as I can access the functions via the
              object?

              As I mentioned above, I have a World object that contains a GUI object.
              The GUI object requires a pointer to a member function ("Display") in
              the World object. Is there a better way to do this?

              Cheers,

              Chris


              cpsmusic@yahoo. com wrote:[color=blue]
              > Thanks for the replies.
              >
              > The link to the "pointers-to-members" page should do the trick.
              >
              > Also, I tried making the member functions static. This worked, but then
              > I couldn't access member variables.
              >
              > Cheers,
              >
              > Chris[/color]

              Comment

              • Paul

                #8
                Re: Passing Member Functions as Function Pointers

                <cpsmusic@yahoo .com> wrote in message
                news:1142688723 .754995.83110@i 39g2000cwa.goog legroups.com...[color=blue]
                > Does this mean that the only way I can get this to work is by passing
                > the object that contains the member functions? In this case, do I need
                > to pass function pointers at all, as I can access the functions via the
                > object?
                >[/color]

                If there is a void* parameter that is provided for you as a "user defined
                data" parameter, you can use the void* as the pointer to the object, and
                then retrieve it again when the function gets called.

                Of course, the instance of the object must still exist.

                Note that this is just one of many ways to do this. The bottom line is that
                you have an object instance, and when the API executes the callback, you
                retrieve that instance in some way so you can get the member data -- this is
                where you have to be creative. That's what it all boils down to.

                How to retrieve the instance can be accomplished in many different ways.
                The simplest being making a global object (as the C++ FAQ illustrates), or
                you can get fancy with maps and handles (this is usually done for OO GUI
                classes), or you can use the void* option if the API's callback allows this.

                - Paul


                Comment

                • BobR

                  #9
                  Re: Passing Member Functions as Function Pointers


                  cpsmusic@yahoo. com wrote in message ...[color=blue]
                  >
                  >Also, I tried making the member functions static. This worked, but then
                  >I couldn't access member variables.
                  >
                  >Cheers,
                  >Chris[/color]

                  I ran into that too. Make the variables 'static' so you can access them from
                  'Display' and 'Keyboard'. There should not be very many. A static var can be
                  accessed by non-static member functions.

                  Otherwise, you need to post a lot more of your code[1] AND the exact errors
                  you are getting, so the experts in this NG can figure it out.

                  [1] - less the OpenGL stuff that does not apply to the problem
                  (glClearColor() , glShadeModel(), etc.). You should separate initialize and
                  draw into to different functions anyway, it will make a difference when you
                  animate. Example below is to show you can access static vars from non-static
                  member functions, not how to do OpenGL (which is off-topic in this NG).

                  // - in class declaration, define outside -
                  // ---------------------------------------
                  static GLfloat RotAngle;
                  static float xspeed; // X Rotation Speed
                  static float yspeed; // Y Rotation Speed
                  static GLfloat AngleX; // Angle For Rotation (deg)
                  static GLfloat AngleY; // Angle For Rotation (deg)
                  // ---------------------------------------
                  // - non-static member function -
                  // ===== DrawGLScene ===== wxWidgets version
                  void BobGLCanvas::Dr awGLScene(){ // Drawing done here
                  wxClientDC dc( this ); // no 'this' if static
                  if( !GetContext() ){ return;}
                  SetCurrent();
                  glClear(GL_COLO R_BUFFER_BIT | GL_DEPTH_BUFFER _BIT);
                  glLoadIdentity( ); // Reset View
                  gluLookAt(0.0, 0.0, ZoomZ, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); //GLfloat
                  ZoomZ(1.50F);
                  glLightfv( GL_LIGHT0, GL_POSITION, lightPosition ); // Set light0
                  position
                  if( xspeed || yspeed ){
                  glRotatef(Angle Y*yspeed, 0.0f, 1.0f, 0.0f); // Rotate On Y axis
                  glRotatef(Angle X*xspeed, 1.0f, 0.0f, 0.0f); // Rotate On X axis
                  }
                  else{
                  glRotatef(RotAn gle, 0.0f, 1.0f, 0.0f); // Rotate on Y
                  axis
                  }
                  Material(MatNum ber); // Material(2) default
                  glCallList( listRockNum + listOffset); // Draw the Rock
                  SwapBuffers(); // Swap Buffers to make rendering visible
                  return;
                  } // DrawGLScene()
                  //---------------------------------------------
                  --
                  Bob R
                  POVrookie


                  Comment

                  • cpsmusic@yahoo.com

                    #10
                    Re: Passing Member Functions as Function Pointers

                    I've implemented the "static" approach and it seems to be working.

                    Cheers,

                    Chris


                    BobR wrote:[color=blue]
                    > cpsmusic@yahoo. com wrote in message ...[color=green]
                    > >
                    > >Also, I tried making the member functions static. This worked, but then
                    > >I couldn't access member variables.
                    > >
                    > >Cheers,
                    > >Chris[/color]
                    >
                    > I ran into that too. Make the variables 'static' so you can access them from
                    > 'Display' and 'Keyboard'. There should not be very many. A static var can be
                    > accessed by non-static member functions.
                    >
                    > Otherwise, you need to post a lot more of your code[1] AND the exact errors
                    > you are getting, so the experts in this NG can figure it out.
                    >
                    > [1] - less the OpenGL stuff that does not apply to the problem
                    > (glClearColor() , glShadeModel(), etc.). You should separate initialize and
                    > draw into to different functions anyway, it will make a difference when you
                    > animate. Example below is to show you can access static vars from non-static
                    > member functions, not how to do OpenGL (which is off-topic in this NG).
                    >
                    > // - in class declaration, define outside -
                    > // ---------------------------------------
                    > static GLfloat RotAngle;
                    > static float xspeed; // X Rotation Speed
                    > static float yspeed; // Y Rotation Speed
                    > static GLfloat AngleX; // Angle For Rotation (deg)
                    > static GLfloat AngleY; // Angle For Rotation (deg)
                    > // ---------------------------------------
                    > // - non-static member function -
                    > // ===== DrawGLScene ===== wxWidgets version
                    > void BobGLCanvas::Dr awGLScene(){ // Drawing done here
                    > wxClientDC dc( this ); // no 'this' if static
                    > if( !GetContext() ){ return;}
                    > SetCurrent();
                    > glClear(GL_COLO R_BUFFER_BIT | GL_DEPTH_BUFFER _BIT);
                    > glLoadIdentity( ); // Reset View
                    > gluLookAt(0.0, 0.0, ZoomZ, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); //GLfloat
                    > ZoomZ(1.50F);
                    > glLightfv( GL_LIGHT0, GL_POSITION, lightPosition ); // Set light0
                    > position
                    > if( xspeed || yspeed ){
                    > glRotatef(Angle Y*yspeed, 0.0f, 1.0f, 0.0f); // Rotate On Y axis
                    > glRotatef(Angle X*xspeed, 1.0f, 0.0f, 0.0f); // Rotate On X axis
                    > }
                    > else{
                    > glRotatef(RotAn gle, 0.0f, 1.0f, 0.0f); // Rotate on Y
                    > axis
                    > }
                    > Material(MatNum ber); // Material(2) default
                    > glCallList( listRockNum + listOffset); // Draw the Rock
                    > SwapBuffers(); // Swap Buffers to make rendering visible
                    > return;
                    > } // DrawGLScene()
                    > //---------------------------------------------
                    > --
                    > Bob R
                    > POVrookie[/color]

                    Comment

                    • Diego Martins

                      #11
                      Re: Passing Member Functions as Function Pointers

                      Personally, I abandoned GLUT and switched to SDL :-)

                      Callbacks suck a lot!

                      Comment

                      • Diego Martins

                        #12
                        Re: Passing Member Functions as Function Pointers

                        Personally, I abandoned GLUT and switched to SDL :-)

                        Callbacks suck a lot!

                        Comment

                        Working...