I am recently saw some code, I am especially not clear of similar function pointer?
and below are function pointer.
I also is confused about below three function, the parameter type is “cairo_output_stream_t”, but the cairo_output_stream_t structure contain member of there three function pointer. I can not understand what below function are doing.
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 is an enum
What’s basically being done is a C-like way to emulate C++’s
thispointer … you pass a pointer to thestructas the first argument to the function call, and from that pointer you can call “methods” of the struct (in this case they are function pointers) and/or access data-members of the struct.So for instance, you might have code using this style of programming that looks something like the following:
Now
method_funccan access thedatamember of themy_structinstance in the same way a C++ class method can access its class-instance non-static data-members through thethispointer.