...
Code Block | ||
---|---|---|
| ||
struct string_mx { size_t size; size_t maxsize; unsigned char strtype; char *cstr; }; typedef struct string_mx *string_m; typedef const struct string_mx *const_string_m; /* Function declarations */ extern errno_t strcpy_m(string_mmx *s1, const_ string_mmx *s2); extern errno_t strcat_m(string_mmx *s1, const_ string_mmx *s2) ; /* etc. */ |
The implementation of the string_mmx
type is fully visible to the user of the data type after including the string_m.h
file. Programmers are consequently more likely to directly manipulate the fields within the structure, violating the software engineering principles of information hiding and data encapsulation and increasing the probability of developing incorrect or nonportable code.
...
This compliant solution reimplements the string_mmx
type as a private type, hiding the implementation of the data type from the user of the managed string library. To accomplish this, the developer of the private data type creates two header files: an external "string_m.h"
header file that is included by the user of the data type and an internal file that is included only in files that implement the managed string abstract data type.
In the external string_m.h
file, the struct string_m
type is declared as a pointer to a struct mx
is typedef to string_mx
datatype, which in turn is declared as an incomplete type.
Code Block | ||
---|---|---|
| ||
struct string_mx; typedef struct string_mx *string_m; typedef const struct string_mx *const_string_m; |
In the internal header file, struct string_mx
is fully defined but not visible to a user of the data abstraction.
Code Block | ||
---|---|---|
| ||
struct string_mx { size_t size; size_t maxsize; unsigned char strtype; char *cstr; }; /* Function declarations */ extern errno_t strcpy_m(string_mmx *s1, const_ string_mmx *s2); extern errno_t strcat_m(string_mmx *s1, const_ string_mmx *s2) ; /* etc. */ |
Modules that implement the abstract data type include both the external and internal definitions, while users of the data abstraction include only the external string_m.h
file. This allows the implementation of the string_mmx
data type to remain private.
...