C ++ passing function as an argument to another function with void pointer

I am trying to pass a function as an argument to another function with a void pointer and not working

#include <iostream> using namespace std; void print() { cout << "hello!" << endl; } void execute(void* f()) //receives the address of print { void (*john)(); // declares pointer to function john = (void*) f; // assigns address of print to pointer, specifying print returns nothing john(); // execute pointer } int main() { execute(&print); // function that sends the address of print return 0; } 

Thing of void function pointers, I could make simpler code like

 #include <iostream> using namespace std; void print(); void execute(void()); int main() { execute(print); // sends address of print return 0; } void print() { cout << "Hello!" << endl; } void execute(void f()) // receive address of print { f(); } 

but i don't know if i can use void pointers

this is for implementing something like this

 void print() { cout << "hello!" << endl; } void increase(int& a) { a++; } void execute(void *f) //receives the address of print { void (*john)(); // declares pointer to function john = f; // assigns address of print to pointer john(); // execute pointer } int main() { int a = 15; execute(increase(a)); execute(&print); // function that sends the address of print cout << a << endl; return 0; } 
+4
source share
2 answers

Using gcc test.cpp , I get:

 test.cpp: In function 'void execute(void* (*)())': test.cpp:12:22: error: invalid conversion from 'void*' to 'void (*)()' [-fpermissive] test.cpp: In function 'int main()': test.cpp:18:19: error: invalid conversion from 'void (*)()' to 'void* (*)()' [-fpermissive] test.cpp:9:6: error: initializing argument 1 of 'void execute(void* (*)())' [-fpermissive] 

The signature for argument f is invalid. You have to use

 void execute(void (* f)()) 

instead of this. Therefore, when assigning john you do not need a cast:

 john = f 

Alternatively, you can simplify this by calling f directly:

 f(); // execute function pointer 

EDIT: Since you want to use void pointers, you need to pass f as the void pointer:

 void execute(void *f) 

Here you will need the john assignment, but since f already void * , you do not need to do the translation.

NOTE: Given that you are passing a pointer to void, the execute function will accept something and you will have runtime errors if you pass the wrong thing. For instance:

 void print_name(const char *name) { printf("%s", name); } void execute1(void *f); void execute2(void (*f)()); int main() { int value = 2; execute1(&value); // runtime crash execute1(&print_name); // runtime crash execute2(&value); // compile-time error execute2(&print_name); // compile-time error } 

Using a specially defined function pointer allows the compiler to generate an error at the point where you passed the wrong type of argument. This is recommended for run-time crashes, because a run-time crashes can be used as a security vulnerability and requires extensive testing so that this error does not occur.

+4
source

Using

void execute(void (*f)()) //receives the address of print

Or better to use:

void execute(boost::function<void()> const & f) //receives the address of print

To accept functors, and also replace boost:: with std:: if you use a compiler that supports C ++ 11

0
source

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


All Articles