I do not recommend using a public structure template. The correct design pattern for OOP in C is to provide access functions to each data, which does not allow public access to data. Class data must be declared in the source in order to be private, and referenced in reverse order, where Create and Destroy select and release the data. Thus, a public / private dilemma will no longer exist.
typedef struct sModuleData module_t' module_t *Module_Create(); void Module_Destroy(module_t *); void Module_SetSomething(module_t *); void Module_GetSomething(module_t *); struct sModuleData { }; module_t *Module_Create() { module_t *inst = (module_t *)malloc(sizeof(struct sModuleData)); return inst; } void Module_Destroy(module_t *inst) { free(inst); }
In another part, if you do not want to use Malloc / Free (which may be an unnecessary overhead for some situations), I suggest you hide the structure in a private file. Private members will be available, but it depends on the proportion of users.
struct sModuleData { }; #include "privateTypes.h" typedef struct sModuleData module_t; void Module_Init(module_t *); void Module_Deinit(module_t *); void Module_SetSomething(module_t *); void Module_GetSomething(module_t *); void Module_Init(module_t *inst) { } void Module_Deinit(module_t *inst) { } int main() { module_t mod_instance; module_Init(&mod_instance); }
Felipe Lavratti Mar 11 '13 at 14:17 2013-03-11 14:17
source share