Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Function pointers within a class

Posted on 2003-03-18
4
Medium Priority
?
805 Views
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();

     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.
0
Comment
Question by:defoman
4 Comments
 
LVL 1

Accepted Solution

by:
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.
0
 
LVL 1

Expert Comment

by:keitha1
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.

0
 

Author Comment

by:defoman
ID: 8164610
Thank you, that was very helpful.
0
 
LVL 1

Expert Comment

by:MattRobb
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 &
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

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

578 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