Link to home
Start Free TrialLog in
Avatar of defoman
defoman

asked on

Function pointers within a class

I am attempting to learn function pointers. I had succes in using this same routine within a C style program (functions and no classes) and now I am trying to implement the same routine as a method in a class.

My class has a public function pointer defined as:
  int (*DS_DrawPixelPtr) (int, int, int, int, int, int, DDSURFACEDESC2 *);

The method within the class is defined as follows:
int DS_Game::DS_SetColorMode(void)
{
     // Determine color mode and set function pointers
     DWORD pixel_format = DS_GetColorMode();

     switch(pixel_format)
     {
          case 15:          // 16 bit 5.5.5 mode
               { DS_DrawPixelPtr = DS_DrawPixel15; } break;
         
          case 16:          // 16 bit 5.6.5 mode
               { DS_DrawPixelPtr = DS_DrawPixel16; } break;

          case 24:          // 24 bit 8.8.8 mode
               { DS_DrawPixelPtr = DS_DrawPixel24; } break;

          case 32:          // 32 bit alpha(8).8.8.8 mode
               { DS_DrawPixelPtr = DS_DrawPixel32; } break;

          default: break;
     }

     // End method
     return(1);
}

Each of the methods DS_DrawPixel15, DS_DrawPixel16, DS_DrawPixel24, and DS_DrawPixel32 are public methods within the same class. They are setup to accept parameters just like the function pointer. When I compile the class, I recieve a conversion error:

  c:\data\dark storm class\ds_game.cpp(644) : error C2440: '=' : cannot convert from 'int (__thiscall DS_Game::*)(int,int,int,int,int,int,struct _DDSURFACEDESC2 *)' to 'int (__cdecl *)(int,int,int,int,int,int,struct _DDSURFACEDESC2 *)'

My question is for someone to explain to me why this call does not work, and what can be done to correct it. I thank you in advance for your assistance.
ASKER CERTIFIED SOLUTION
Avatar of frogger1999
frogger1999

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of keitha1
keitha1

Also, just for giggles, look up pointer-to-member in the Mickysoft help. The syntax is a little weird but it is an alternative to static functions. In general use frogger1999's method.

Avatar of defoman

ASKER

Thank you, that was very helpful.
the answer to your question you can find in the error message: you can't access class members
without reference or pointer to a class.

the pointer you declared above, can be used only with static class members.

consider the use of the following operators used with pointer to class members:

::*     - declaration of pointer to member
.*     - dereference of pointer to member using an object or object reference
->*     - dereference of pointer to member using a pointer to an object

in your case pointer must be declated as follows:

int (DS_Game::*DS_DrawPixelPtr) (int, int, int, int, int, int, DDSURFACEDESC2 *);

in order to initialize or assign a new value a pointer to function must be taken with &
operator:
     DS_DrawPixelPtr = &DS_Game::DS_DrawPixel16;
     
then use one of the dereference operators to execute your function via pointer:
 
     DS_Game     g, *pg;
     
     ...

     (g.*DS_DrawPixelPtr)(...);
     (pg->*DS_DrawPixelPtr)(...);

     or
     
     (this->*DS_DrawPixelPtr)(...);

     if you use it inside a class member function