oop - Implementing data hiding access specifiers in C language -


is there way implement access specifiers "private", "protected" in c language. came across solutions in internet using "static" , "ifdefs" making function available inside other functions.

apart these, there c implementation equivalent of using private , protected access specifiers in c++ classes?

c not have user definable name spaces or access specifiers. since exclude (ab)use of preprocessor, way compiler error trying access private parts of "classes" not have .h file exposes "private" stuff. can still put "private" separate .h files (included module's or library's own .c files, not meant included application code), or hidden behind #ifdefs (requiring special define activate "private" parts).

one common way hide things use opaque structs aka opaque pointers. approach, code outside module or library has pointer struct, no struct definition. , uses functions offered module instance, access it, , release it.

with approach, public interface: functions provide in public .h file, public support structs have definition there. private interface code full struct definition visible, , functions not in public .h file.

protected access implies inheritance, works differently c++, when implemented c hand, , broad subject cover in answer. closest thing have several .h files, provide several levels of "public" access, , responsibility of programmer not problems them.

the thing approach is, other code using module not need modified (or recompiled), if struct changed. struct might union, , module's functions branch based on actual type, invisibe code using it. thing is, module can control creation of structs, example have pool of structs , avoid using heap, invisible application code. 1 downside is, can't have inline functions (because inline function body in .h file need struct definition, trying hide here), prevents nice compiler optimizations in cases performance concern.

example (untested code written answer):

module.h:

// ...other standard header file stuff ...  // forward declaration of struct struct module_data;  // "constructor" function struct module_data *module_initialize_data(int value);  // modification function int module_update_data(struct module_data *data, int adjust);  // "destructor" function void module_release(struct module_data *data); 

module.c

#include "module.h"  // struct definition in .c file struct module_data {    int value; };  struct module_data *module_initialize_data(int value) {     struct module_data *data = malloc(sizeof(*data));     data->value = value;     return data; }  int module_update_data(struct module_data *data, int adjust) {     data->value += adjust;     return data->value; }  void module_release(struct module_data *data) {     free(data); } 

relevant wikipedia links reference:


Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -