Function pointers within a class

Posted on 2003-03-18
Medium Priority
Last Modified: 2008-03-17
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();

          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

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.
Question by:defoman
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

frogger1999 earned 200 total points
ID: 8164395
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.

Expert Comment

ID: 8164463
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.


Author Comment

ID: 8164610
Thank you, that was very helpful.

Expert Comment

ID: 8164676
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 &
     DS_DrawPixelPtr = &DS_Game::DS_DrawPixel16;
then use one of the dereference operators to execute your function via pointer:
     DS_Game     g, *pg;



     if you use it inside a class member function


Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question