typedef & function pointer
typedef & function pointer
我最近看到一些代码,我特别不清楚类似的函数指针?
下面是函数指针。
我也对下面的三个函数感到困惑,参数类型是"cairo_output_stream_t",但cairo_output_stream_t结构包含三个函数指针的成员。我不明白下面的函数在做什么。
typedef cairo_status_t
(*cairo_output_stream_write_func_t) (cairo_output_stream_t *output_stream,
const unsigned char *data,
unsigned int length);
typedef cairo_status_t
(*cairo_output_stream_flush_func_t) (cairo_output_stream_t *output_stream);
typedef cairo_status_t
(*cairo_output_stream_close_func_t) (cairo_output_stream_t *output_stream);
struct _cairo_output_stream {
cairo_output_stream_write_func_t write_func;
cairo_output_stream_flush_func_t flush_func;
cairo_output_stream_close_func_t close_func;
unsigned long position;
cairo_status_t status;
int closed;
};
cairo_status_t是一个枚举
基本上正在做的是一种类似 C 的方式来模拟C++的this
指针......将指向struct
的指针作为函数调用的第一个参数传递,从该指针可以调用结构的"方法"(在本例中它们是函数指针)和/或访问结构的数据成员。
例如,您可能有使用这种编程风格的代码,如下所示:
struct my_struct
{
unsigned char* data;
void (*method_func)(struct my_struct* this_pointer);
};
struct my_struct object;
//... initialize the members of the structure
//make a call using one of the function pointers in the structure, and pass the
//address of the structure as an argument to the function so that the function
//can access the data-members and function pointers in the struct
object.method_func(&object);
现在,method_func
可以访问my_struct
实例的data
成员,就像C++类方法可以通过this
指针访问其类实例非静态数据成员一样。
相关文章:
- "error: no matching function for call to"构造函数错误
- 调用专用模板时出错"no matching function for call to [...]"
- C++ - "!pointer"和"pointer == nullptr"的区别?
- 库函数需要一个 std::function<void(void)>,如何传入类函数?
- Confusion: decltype vs std::function
- 为什么 std::function 可以作为 std::not2 的参数?
- 'max'匹配'std::function<const int &(const int &, const int &)>'无过载
- 传递给std::function template的template参数究竟代表什么
- "Called Object Type is Not a Function or Pointer" 与 typedef 和类
- 是否可以定义"template function pointer"的类型?
- 在 2D 阵列分配期间对 calloc 进行"expression must have (pointer-to) function type"
- "Called object 'int' is not a function or a function pointer"错误
- 为什么隐式"lambda to function pointer conversion"禁止"by reference"捕获静态成员?
- 'variable' 不是类型 'pointer to member function' 的有效模板参数
- C++ 中的"function Pointer"与 C# 中的"delegate"行为相似吗?
- 何时使用"pointer to function"和"function pointer"?
- pre-typedef'ing a variadic-function-pointer 参数
- 在 Python 中传递"pointer to a virtual function"作为参数
- typedef & function pointer
- Pointer-to-member-function error