简体   繁体   中英

Void pointer and casting in function argument

Here is a code snippet.

class NetworkRequestChannel {
public:
  ...

  NetworkRequestChannel(const unsigned short _port_number,
    void * (*connection_handler) (int *));
  ...
private:
  ...
}

My question is about the argument void * (*connection_handler) (int *) . I have a vague idea how to read that, and it's as a void pointer to an object named connection_handler that is being then cast to an int pointer. But I know that's almost certainly wrong, and I'm not sure how else to interpret it.

The argument

void * (*connection_handler) (int *)

Is a pointer to a function taking one argument of type int* and returning a void*

It's the syntax for a function pointer. The parameter in question accepting a function that takes a single int* as an argument and returns a void* value (a generic memory address).

You can read more about it here .

(That's quite an old article, and I encourage people to comment or edit this answer to list ones that emphasize modern C++ practices. Disregard this if the article I linked is actually up to date with the best practices!)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM