0

Im interested in creating arrays of functions which don't take arguments and don't return anything.

Is there a way to create a generic function pointer and execute it?

Example:

ptr ptrArray[1];
PtrArray[0] = &myfunc;

More generally does this work if myfunc is a method in a specifc object? Part of me assumes not since functions are shared across multiple objects in code.

For example, you have a list of objects all with an update method, you want each one executed.

Peter - Reinstate Monica
  • 15,048
  • 4
  • 37
  • 62
FourierFlux
  • 439
  • 5
  • 13
  • You may want to read some of the answers [here](https://stackoverflow.com/a/1487507/2115408). The general view here is that to call member methods, you also need to provide a `this` so the object is known, or to use `std::function()` if you can. – mmixLinus Jan 26 '22 at 09:25
  • 1
    In short, use `std::function`. The term "function pointers" means exactly `void (*)()` in your case. It doesn't include all the other stuff. – Passer By Jan 26 '22 at 09:29
  • "Call an update method on all objects" also sounds like the prototypical use case for (runtime or compile time) polymorphism. If you *know* that all objects have something in common -- they can be drawn or updated or digest messages or whatever -- you express that in interfaces they implement. Then you don't store pointers to functions but pointers to those interfaces, pointing to objects of various concrete types who have this specific member function in common, and call that `update` function. OO is *object* oriented ;-). – Peter - Reinstate Monica Jan 26 '22 at 09:37
  • E.g. `struct Updatable_I { void update() = 0; }; struct my_T: public Updatable_I { void update() { cout << "update\n"; }} myObj; Updatable_I *updatebleArr[] = { &myObj }; for( auto u: updatebleArr) { u->update(); }`. – Peter - Reinstate Monica Jan 26 '22 at 09:43
  • Thanks, I'm not really following. I figured out how to do this without objects but now I really want to call object methods. Is there some way to store an array which houses the method and object it belongs to in a generic way? – FourierFlux Jan 27 '22 at 04:03

1 Answers1

1

std::function seems a way to go, as it supports the largest variety of function objects, including lambdas, ones created by bind etc.

#include <functional>
#include <iostream>
#include <array>

struct S
{
    void doStuff() {std::cout << "DoStuffMember " << this << "\n";}
    void doStuff() const {std::cout << "DoStuffMemberConst " << this << "\n";}
};

void doStuff() {std::cout << "DoStuffFree\n";}

int main()
{
    S s;
    std::string_view sv{"local var\n"};
    std::array<std::function<void()>, 8> fkns;
    fkns[0] = [&s]{ s.doStuff();};
    fkns[1] = [&sr=std::as_const(s)]{sr.doStuff();};
    fkns[2] = std::bind<void (S::*)()>(&S::doStuff, s);
    fkns[3] = std::bind<void (S::*)()>(&S::doStuff, std::ref(s));
    fkns[4] = std::bind<void (S::*)() const>(&S::doStuff, std::cref(s));
    fkns[5] = [sv]{std::cout << "lambda capturing " << sv;};
    fkns[6] = []{std::cout << "lambda\n";};
    fkns[7] = doStuff;

    for (auto&& f: fkns) {
        f();
    }

    return 0;
}

Demo

alagner
  • 3,448
  • 1
  • 13
  • 25
  • I honestly don't understand this, can you direct me to some more information? More generally I haven't been satisfied so far with what I have found - basically I want to create a dynamic array of objects which reside inside an ISR and each execute a particular function I want. I want this generalize to multiple ISR with different functions so I can't simply create a base class, because there will be different update functions for given circumstance. – FourierFlux Jan 27 '22 at 03:02