Sign in to follow this  

Using Function Pointers in class declaration

This topic is 3639 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi I recently write a class that manage DirectX surfaces. In my class i must choose a proper function upon on a color mode. For example if display color-mode was 16-Bit the class must choose putPixel16() member function and if display color-mode was 24-Bit choose putPixel24() member function. The class might be : class CSurface { private: void putPixel16(int x, int y, int clr); void putPixel24(int x, int y, int clr); public: void (*putPixel)(int x, int y, int clr); }; For this i decide to use "Function Pointer" but i dont really know how use it and if it's true...? If i can use "Function Pointer" i must assign putpixel public member function to one of 2 private member function putPixel16() and putPixel24(). Then if you want to plot a pixel on surface you can use public member putPixel that basically is a "Function Pointer". Is my style true for this reason...? If you have some idea please send to topic. Thank you.

Share this post


Link to post
Share on other sites
It looks like you have the right idea, more or less. You would assign a value to the function pointer like this:
putPixel = &CSurface::putPixel16;
And invoke the function like this:
this->*putPixel(...);
I don't work with this syntax much these days, so the above may or may not be exactly correct (just Google 'C++ member function pointers' and I'm sure you'll find some other code examples to refer to).

Now, calling a function through a pointer on a per-pixel basis sounds a bit iffy to me, but then again I don't know what you're doing exactly. Just be aware that there are probably other ways you could approach the problem (e.g. templates).

Share this post


Link to post
Share on other sites
Sign in to follow this