简体   繁体   English

空指针和函数参数中的转换

[英]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 *) .我的问题是关于参数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.我对如何阅读它有一个模糊的想法,它是一个指向名为connection_handler的对象的空指针,然后将其转换为 int 指针。 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*是一个指向一个函数的指针,该函数采用一个int*类型的参数并返回一个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).有问题的参数接受一个函数,该函数将单个int*作为参数并返回一个void*值(通用内存地址)。

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!) (这是一篇很老的文章,我鼓励人们评论或编辑此答案以列出强调现代 C++ 实践的答案。如果我链接的文章实际上是最新的最佳实践,请忽略这一点!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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