How to reassign function pointer in C ++

First, I'm very new to pointer functions and their terrible syntax, so play well.

I am writing a filtering method for all the pixels in my bitmap based on the function I pass. I wrote a method to dereference this method and call it in a pixel buffer, but I also need a wrapper method in my bitmap class that takes a pointer to a function and passes it. How can I do it? What is the syntax? I am a little puzzled.

Here is my code with all the mismatched bits, split and concatenated files (read all initialized variables, etc.).

struct sColour { unsigned char r, g, b, a; }; class cPixelBuffer { private: sColour* _pixels; int _width; int _height; int _buffersize; public: void FilterAll(sColour (*FilterFunc)(sColour)); }; void cPixelBuffer::FilterAll(sColour (*FilterFunc)(sColour)) { // fast fast fast hacky FAST for (int i = 0; i < _buffersize; i++) { _pixels[i] = (*FilterFunc)(_pixels[i]); } } class cBitmap { private: cPixelBuffer* _pixels; public: inline void cBitmap::Filter(sColour (*FilterFunc)(sColour)) { //HERE!! } }; 
+4
source share
3 answers

If I understand what you want:

 inline void cBitmap::Filter(sColour (*FilterFunc)(sColour)) { _pixels->FilterAll( FilterFunc); } 

Often the use of function pointers can be made easier to read if you use a typedef for the type of the function pointer (yours is not really that bad in itself - they can get a lot worse):

 struct sColour { unsigned char r, g, b, a; }; typedef sColour (*FilterFunc_t)(sColour); // typedef for a FilterFunc class cPixelBuffer { private: sColour* _pixels; int _width; int _height; int _buffersize; public: void FilterAll(FilterFunc_t FilterFunc); }; void cPixelBuffer::FilterAll(FilterFunc_t FilterFunc) { // fast fast fast hacky FAST for (int i = 0; i < _buffersize; i++) { _pixels[i] = (*FilterFunc)(_pixels[i]); } } class cBitmap { private: cPixelBuffer* _pixels; public: inline void cBitmap::Filter(FilterFunc_t FilterFunc) { _pixels->FilterAll( FilterFunc); } }; 
+4
source

Boost libraries can make your life easier. see boost function .
For example, here is a function that executes a callback function that takes two int and returns int :

 void do_something( boost::function<int (int, int)> callback_fn ); 

Then it can be used as a normal function:

 int result = callback_fn(1,2); 

Pass it do_something like this:

 boost::function<int (int, int)> myfn = &the_actual_fn; do_something(myfn); 

With the boost function, you can also easily pass functions to a member of the class (see boost bind ).
Good luck with your program.

+3
source

You can make it all the more understandable by using typedef for your type of function pointer:

 typedef sColour (*FilterFunc_t)(sColour) void FilterAll(FilterFunc_t FilterFunc); 

Passing a variable containing a pointer to another function works the same way as passing any other variable:

 inline void cBitmap::Filter(FilterFunc_t FilterFunc) { FilterAll(FilterFunc); } 
+2
source

Source: https://habr.com/ru/post/1306188/


All Articles