c++ - Void pointer initialization? If not, what is it? -


i'm trying understand segment of code used parameter in c++ can't seem find example of elsewhere on internet. here's segment:

void (*cb)(void) 

is way of initializing void pointer? benefit of doing versus void *cb?

in example, cb pointer function takes no arguments , has no return value

for example if have

void printhello( ) {     cout << "hello" << endl; } 

then later have

void (*cb)(void); cb = printhello; 

i can call function using:

cb();  

which call printhello();

the utility of can assign different functions cb , call them , pass them around other functions other pointer variable.

often clarity, programmers create specific type avoid having write mouthful:

typedef void (*tprttovoidfn)(void); tptrtovoidfn  cb; cb = printhello; 

for comparison, pointer function returns int like:

int (*ptrtofunctionreturningint)(void); 

and pointer function taking int , returning nothing like:

void (*ptrtofunctionreturningnothing)(int); 

Comments

Popular posts from this blog

android - How to save instance state of selected radiobutton on menu -

python 3 IndexError: list index out of range -

IF statement in MySQL trigger -