How can you call a function from only a pointer to the function. For example, assuming the function has a return type of void:
void print()
{
std::cout << "Hello World!" << std::endl;;
}
void run_func(void* func)
{
func(); // what im trying to do (doesnt actually work)
}
int main()
{
run_func(print);
}
Expected output:
Hello World!
It’s a bit like how std::thread creates a thread from the pointer of a variable.
>Solution :
void print()
{
std::cout << "Hello World!" << std::endl;;
}
// Change your parameter as follows. You need to wrap function
// pointers in parantheses.
// void run_func(void *func())
void run_func(void (*func)())
{
func(); // what im trying to do (and now works)
}
int main()
{
run_func(print);
}