Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to interpret "void(*)()"?

When I read the shared_ptr, I found a piece of code:

void(*)()

How to interpret it?

like image 507
dudengke Avatar asked Jan 25 '23 17:01

dudengke


1 Answers

It is a pointer to a function type, which can be used for all the functions which have no arguments and returns void.

For example:

void function_1() {}
void function_2() {}

void(*func_1_ptr)() = function_1; // or = &function_1;
void(*func_2_ptr)() = function_2; // or = &function_2;

Now func_1_ptr holds the pointer to the function function_1, and func_2_ptr holds the pointer to function_2.

You can make the type more intuitive by using declaration.

using FunPtrType = void(*)();

and now you could write

FunPtrType  func_1_ptr = function_1; // or = &function_1;
//Type      identifier   function
FunPtrType  func_2_ptr = function_2; // or = &function_2;
like image 81
JeJo Avatar answered Jan 29 '23 21:01

JeJo