C ++ Function Pointer as an argument and classes?

Hmm, I read a few guides on this, but I can't figure it out, how to use function pointers in C ++ correctly?

I have a class that I want to call a function after it has finished everything that it is doing at the moment, for example:

class WindowImages
{
    public:
        void Update()
        {
            for(unsigned int i = 0; i < _images.size(); i++)
            {
                //Do something
                _images[i]->MyFunctionPointer();
            }
        }
        void Add(Image* image, void (*func)(void))
        {
            image->function = func; //this is wrong
            _images.push_back(image);
        }         

    private:
        vector<Image*> _images;
}

class Image
{
    public:
        void ImageClicked();

        void *function(void);
};

void main()
{
    Image* image = new Image();

    WindowImages images;
    images.Add(image, image->ImageClicked);
}

I'm trying to add an image to a vector and pass a function as an argument that will be called when the image class finishes doing everything it needs to do with each image. The functions will be different for each image, but they will all be in the void function () format .

, , ? , ... , ( , , ++ 11 lambdas), !

+4
3

. -, . , , .

- Image void, :

void (Image::*function)(). 

- , ->* .* . :

// declare a pointer to Image member function
void (Image::*function)();

// assign address of member function
function = &Image::ImageClicked;

// uses ->* for access
Image *pObj = new Image();
(pObj->*function)();

// uses .* for access
Image image;
(image.*function)();

,

+3

, , ++, function pointer underhood. ++ , .

#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
void f1() {
    cout << "f1()" << endl;
}
void f2() {
    cout << "f2()" << endl;
}
void f3() {
    cout << "f3()" << endl;
}    
class A {
public:
    void (*fp)();
};

class B {
public:
    void add(A* a, void (*fp)()) {
        a->fp = fp;
        vec_a.push_back(a);
    }
    ~B() {
        for (int i = 0; i < vec_a.size(); ++i) {
            vec_a[i]->fp();
        }
    }
private:
    vector<A*> vec_a;
};

int main() {
    B *b = new B();
    A* a[3];
    a[0] = new A();
    a[1] = new A();
    a[2] = new A();
    b->add(a[0], f1);
    b->add(a[1], f2);
    b->add(a[2], f3);
    delete b;
}

f1()
f2()
f3()
0

, ++. , .

When working with function pointers, I find typedefs really useful to keep things organized. This somewhat contrived example does roughly what you are trying to do ...

#include <stdio.h>
#include <vector>

typedef void (*fptr_type) (int);

void printSquare (int i) {
   printf ("%d\n", i * i);
   return;
}

void printDouble (int i) {
   printf ("%d\n", 2 * i);
   return;
}

class IntFPtrClass {
   public:
      int intVal;
      fptr_type fptr;

      IntFPtrClass (int i, fptr_type f) : intVal(i), fptr(f) { }

   // Just to show that static methods can be used as function pointers.
   static void printPlusOne(int i) {
      printf ("%d\n", i + 1);
      return;
   }
};

int main (void)
{
   std::vector<IntFPtrClass> my_vector;

   my_vector.push_back (IntFPtrClass(3, printSquare));
   my_vector.push_back (IntFPtrClass(5, printDouble));
   my_vector.push_back (IntFPtrClass(7, IntFPtrClass::printPlusOne));

   for (int i = 0; i < my_vector.size(); ++i)
   {
      my_vector[i].fptr(my_vector[i].intVal);
   }

   return 0;
}
-2
source

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


All Articles