4 ms·
> I'm continually surprised by how often people insist on putting struct definitions right in their header files, defeating the whole idea of type abstraction.
by rmind 9y ago
> I'm continually surprised by how often people insist on putting struct definitions right in their header files, defeating the whole idea of type abstraction.
In general, I totally agree with you that it is best to keep the structure definition in the translation unit (the .c file) and opaque structure declaration in the header. However, there are some cases when it is not very practical, e.g. when you you want to embed the structure into another (for performance reasons or cases like on-disk structures, etc) or when you want to separate out some logic into another .c file (think of separation of concerns), instead of having one massive .c file.
When there is such need, I generally create two headers files, e.g. foo.h (for the public API) and foo_impl.h (structure definition and related stuff), with something like:
#if !defined(__FOO_PRIVATE)
#error "foo_impl.h should only be included by the internal foo modules"
#endif
- yawaramin 9y agoGood points. It's at least possible to break up a massive .c file into multiple smaller .c files amalgamated with #includes, right? The foo_impl.h idea is a good one also.