The natural way to do OOP in C is to do something like this:
#include <stddef.h>
/* stream operations */
struct _src {
/** open stream. Return true on success */
bool (*open)(struct _src *src, const char *fspec);
/** close stream. Return true on success */
bool (*close)(struct _src *src);
/** Returns the actual size of buffer read (<=len), or 0 upon error */
size_t (*read)(struct _src *src, void *buf, size_t len);
/* hold the actual FILE* in the case of file stream */
void *data;
};
Sometime I've seen that the operation(s) are in a separate struct, and design in like this:
#include <stddef.h>
struct _src {
/* vtable */
const struct _src_ops *ops;
/* hold the actual FILE* in the case of file stream */
void *data;
};
/* stream operations */
struct _src_ops {
/** open stream. Return true on success */
bool (*open)(struct _src *src, const char *fspec);
/** close stream. Return true on success */
bool (*close)(struct _src *src);
/** Returns the actual size of buffer read (<=len), or 0 upon error */
size_t (*read)(struct _src *src, void *buf, size_t len);
};
What's the whole point of doing one way or the other ?