Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in return a function pointer in C++ without typedef

I'm trying to return a pointer to function without the use of typedef, but the compiler (gcc) is emitting a strange error, as if I could not do that kind of setting.

Remarks: With the use of typedef code works.

code:

void catch_and_return(void (*pf)(char*, char*, int&), char *name_one, char* name_two, int& number)(char*, char *, int&)
{
    pf(name_one, name_two, number);

    return pf;
}

Error:

'catch_and_return' declared as function returning a function

Can you explain to me why the compiler does not let me do this? Thank you!

like image 953
Wilson Junior Avatar asked Mar 24 '23 00:03

Wilson Junior


1 Answers

Declare your function as the following:

void (*catch_and_return(void (*pf)(char*, char*, int&), char *name_one, char* name_two, int& number))(char*, char *, int&)
{
    pf(name_one, name_two, number);

    return pf;
}

The syntax for functions that returns functions is:

returned-function-return-type (* function-name (parameter-list) ) (function-to-return-parameter-list)

Note: This declarations can be cumbersome to understand at first sight, use typedef whenever is possible

like image 150
higuaro Avatar answered Apr 27 '23 15:04

higuaro