How can I use an array of function pointers?
How should I use array of function pointers in C? How can I initialize them? 12 Answers 12
How should I use array of function pointers in C? How can I initialize them? 12 Answers 12
I just saw a picture today and think I’d appreciate explanations. So here is the picture: I found this confusing and wondered if such codes are ever practical. I googled the picture and found another picture in this reddit entry, and here is that picture: So this “reading spirally” is something valid? Is this how … Read more
Is there a way to pass a call back function in a Java method? The behavior I’m trying to mimic is a .Net Delegate being passed to a function. I’ve seen people suggesting creating a separate object but that seems overkill, however I am aware that sometimes overkill is the only way to do things. … Read more
For one class I want to store some function pointers to member functions of the same class in one map storing std::function objects. But I fail right at the beginning with this code: #include <functional> class Foo { public: void doSomething() {} void bindFunction() { // ERROR std::function<void(void)> f = &Foo::doSomething; } }; I receive … Read more
Why do the following work? void foo() { cout << “Foo to you too!\n”; }; int main() { void (*p1_foo)() = foo; void (*p2_foo)() = *foo; void (*p3_foo)() = &foo; void (*p4_foo)() = *&foo; void (*p5_foo)() = &*foo; void (*p6_foo)() = **foo; void (*p7_foo)() = **********************foo; (*p1_foo)(); (*p2_foo)(); (*p3_foo)(); (*p4_foo)(); (*p5_foo)(); (*p6_foo)(); (*p7_foo)(); } 5 … Read more
I have always been a bit stumped when I read other peoples’ code which had typedefs for pointers to functions with arguments. I recall that it took me a while to get around to such a definition while trying to understand a numerical algorithm written in C a while ago. So, could you share your … Read more
I’m trying to store in a std::tuple a varying number of values, which will later be used as arguments for a call to a function pointer which matches the stored types. I’ve created a simplified example showing the problem I’m struggling to solve: #include <iostream> #include <tuple> void f(int a, double b, void* c) { … Read more
Is it possible to pass a lambda function as a function pointer? If so, I must be doing something incorrectly because I am getting a compile error. Consider the following example using DecisionFn = bool(*)(); class Decide { public: Decide(DecisionFn dec) : _dec{dec} {} private: DecisionFn _dec; }; int main() { int x = 5; … Read more
I have a method that’s about ten lines of code. I want to create more methods that do exactly the same thing, except for a small calculation that’s going to change one line of code. This is a perfect application for passing in a function pointer to replace that one line, but Java doesn’t have … Read more
In C++, when and how do you use a callback function? EDIT: I would like to see a simple example to write a callback function. 11 Answers 11