Suppose I have a function
void myFun(int*)
In C++ what exactly does the following mean
( void(*)(void*) )&myFun
Is it a pointer to a function that takes a (void*) as an argument and returns a void? Is this type of cast permitted?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
As it stands, I’m pretty sure it’s just not allowed.
If you remove the parens around the initial
voidto get:…then yes, it’s a pointer to a function returning void and taking a pointer to void as its only argument. To cast to that type, you need to enclose the entire name of the type in parentheses, so you’d get:
(void (*)(void *)), which you’d follow by the value being cast, to get:At least if memory serves, yes, this is allowed, though dereferencing the pointer (i.e., attempting to call the function it points at) via the result may give undefined behavior. In particular, when/if you call the function, it’s expecting a pointer to int, and will (presumably) use whatever it points at as an int. If, however, what it points at isn’t properly aligned to be used as an int, it’s not likely to work as expected.