简体   繁体   English

如何解释以下typedef语句

[英]How can I interpret the following typedef statement

How can I interpret following typedef statement. 如何解释以下typedef语句。

I already have some knowledge through type convertions. 我已经通过类型转换获得了一些知识。 They are used to reinterpret the bytes behind a variable as another type. 它们用于将变量后面的字节重新解释为另一种类型。 Example: 例:

unsigned char* byte = (unsigned char*) (some signed char);

But now I see following statement: 但现在我看到以下声明:

typedef void (*uv_read_cb)(uv_stream_t* stream, ssize_t nread, uv_buf_t buf);

https://github.com/bodokaiser/libuv/blob/master/include/uv.h#L314 https://github.com/bodokaiser/libuv/blob/master/include/uv.h#L314

What totally confuses me are: 让我困惑的是:

  • there is no name of the typedef 没有typedef的名称
  • how can we do a type conversion on some arguments??? 我们怎样才能对某些参数进行类型转换?

Has somebody an answer on these? 有人对这些问题有答案吗?

The typedef does have a name... uv_read_cb which is a pointer to a function that accepts a uv_stream_t* , a ssize_t and a uv_buf_t argument and returns void . typedef确实有一个名称... uv_read_cb ,它是一个指向接受uv_stream_t*ssize_tuv_buf_t参数并返回void的函数的指针。

This allows you to add things like a pointer to a function in a structure, passing function pointers to functions, etc., while allowing you to define what type of function can be assigned to the variable or passed... 这允许您添加诸如指向结构中的函数的指针,将函数指针传递给函数等,同时允许您定义可以为变量分配的函数类型或传递...

typedef void (*uv_read_cb)(uv_stream_t* stream, ssize_t nread, uv_buf_t buf);

struct myVTable
{
    uv_read_cb  uv_read_callback;
};

void myVFunction(  uv_stream_t* stream, uv_read_db callback )
{
     ssize_t length = 100;
     uv_buf_t buf;
     myVTable table;

     table.uv_read_callback = callback;

     table.uv_read_callback( stream, length, buf );

     // or you could alternatively use 'callback( stream, length, buf );'
}

It's a pointer to a function that gets uv_stream_t* , ssize_t and uv_buf_t , and returns void. 它是一个指向获取uv_stream_t*ssize_tuv_buf_t的函数的指针,并返回void。 uv_read_cb is also the type, and you can use it to define other functions with the same signature. uv_read_cb也是类型,您可以使用它来定义具有相同签名的其他函数。 For example: 例如:

uv_read_cb my_func;

此语句将uv_read_cb定义为指向返回void的函数的指针。

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

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