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.
defomanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

frogger1999Commented:
It is becuase it is a member function not a static class function.  Try declaring DS_DrawPixel15 etc as

static int DS_DrawPixel15(int, int, int, int, int, int, DDSURFACEDESC2 *);

static is the most important part up there.
 
Basically what is happening is that for a class function there is an implicit first parameter "this".  

That is what the __thiscall refers to.  If you absolutely must have the instance of your class call member functions then you should declare the function like this

static int DS_Game::DS_DrawPixel15(DS_Game *, int, int, int, int, int, int, DDSURFACEDESC2 *);

and then pass the instance in manually.


Short answer:  when working with function pointers use static functions or C style functions outside your class.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
keitha1Commented:
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.

0
defomanAuthor Commented:
Thank you, that was very helpful.
0
MattRobbCommented:
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
     
     

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.