function-pointers

How does dereferencing of a function pointer happen?

荒凉一梦 提交于 2019-11-25 23:16:35
问题 Why and how does dereferencing a function pointer just \"do nothing\"? This is what I am talking about: #include<stdio.h> void hello() { printf(\"hello\"); } int main(void) { (*****hello)(); } From a comment over here: function pointers dereference just fine, but the resulting function designator will be immediately converted back to a function pointer And from an answer here: Dereferencing (in way you think) a function\'s pointer means: accessing a CODE memory as it would be a DATA memory.

Calling C++ class methods via a function pointer

怎甘沉沦 提交于 2019-11-25 23:16:24
问题 How do I obtain a function pointer for a class member function, and later call that member function with a specific object? I’d like to write: class Dog : Animal { Dog (); void bark (); } … Dog* pDog = new Dog (); BarkFunction pBark = &Dog::bark; (*pBark) (pDog); … Also, if possible, I’d like to invoke the constructor via a pointer as well: NewAnimalFunction pNew = &Dog::Dog; Animal* pAnimal = (*pNew)(); Is this possible, and if so, what is the preferred way to do this? 回答1: Read this for

Casting a function pointer to another type

旧巷老猫 提交于 2019-11-25 23:14:36
问题 Let\'s say I have a function that accepts a void (*)(void*) function pointer for use as a callback: void do_stuff(void (*callback_fp)(void*), void* callback_arg); Now, if I have a function like this: void my_callback_function(struct my_struct* arg); Can I do this safely? do_stuff((void (*)(void*)) &my_callback_function, NULL); I\'ve looked at this question and I\'ve looked at some C standards which say you can cast to \'compatible function pointers\', but I cannot find a definition of what \

Function pointer to member function

女生的网名这么多〃 提交于 2019-11-25 22:21:54
问题 I\'d like to set up a function pointer as a member of a class that is a pointer to another function in the same class. The reasons why I\'m doing this are complicated. In this example, I would like the output to be \"1\" class A { public: int f(); int (*x)(); } int A::f() { return 1; } int main() { A a; a.x = a.f; printf(\"%d\\n\",a.x()) } But this fails at compiling. Why? 回答1: The syntax is wrong. A member pointer is a different type category from a ordinary pointer. The member pointer will

“unpacking” a tuple to call a matching function pointer

守給你的承諾、 提交于 2019-11-25 22:19:03
问题 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) { std::cout << a << \":\" << b << \":\" << c << std::endl; } template <typename ...Args> struct save_it_for_later { std::tuple<Args...> params; void (*func)(Args...); void

Passing capturing lambda as function pointer

冷暖自知 提交于 2019-11-25 21:53:50
问题 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; Decide greaterThanThree{ [x](){ return x > 3; } }; return 0; } When I try to compile this, I get the following compilation error: In function \'int main()\': 17:31: error: the

How do function pointers in C work?

北战南征 提交于 2019-11-25 21:36:40
问题 I had some experience lately with function pointers in C. So going on with the tradition of answering your own questions, I decided to make a small summary of the very basics, for those who need a quick dive-in to the subject. 回答1: Function pointers in C Let's start with a basic function which we will be pointing to : int addInt(int n, int m) { return n+m; } First thing, let's define a pointer to a function which receives 2 int s and returns an int : int (*functionPtr)(int,int); Now we can

How can I pass a member function where a free function is expected?

此生再无相见时 提交于 2019-11-25 20:35:22
The question is the following: consider this piece of code: #include <iostream> class aClass { public: void aTest(int a, int b) { printf("%d + %d = %d", a, b, a + b); } }; void function1(void (*function)(int, int)) { function(1, 1); } void test(int a,int b) { printf("%d - %d = %d", a , b , a - b); } int main (int argc, const char* argv[]) { aClass a(); function1(&test); function1(&aClass::aTest); // <-- How should I point to a's aClass::test function? return 0; } How can I use the a 's aClass::test as an argument to function1 ? I'm stuck in doing this. I would like to access a member of the