I am writing a static library for C/C++ (personal project). As my headers became
ID: 655574 • Letter: I
Question
I am writing a static library for C/C++ (personal project). As my headers became rather large, I thought that splitting my headers would be a good idea.
Right now a header looks like this:
MainClass.hpp:
namespace impl: implementation details enclosed in a namespace impl
forward declarations of classes declared below (when needed)
MainClass declaration
MainClass free functions declarations
MainClass specialization declaration
MainClass specialization free function declarations
AuxiliaryClass1 declaration
AuxiliaryClass1 free functions declarations
Explanation / Answer
You can do the following:
split the code into one class per header file.
in the case of templates, the implementation of the class functions can/should reside in the same header file (there is probably no scenario when you will want include the header, but not those definitions).
dependencies (your AuxiliaryClassK) should be #included instead of being defined directly. This makes your headers smaller, promotes code reuse (as you can include them - and only them - elsewhere) and keeps your code cleaner.
split your code into directories. The directory structure is normally a matter of policy; You could use your impl folder idea, or (for example) you could group them like boost does (one directory per functional area with all the implementation files, and one include file for convenience, which you can include in a single line in client code).
use pimpl idiom to minimize compile times.
As a rule, I would not use _Class file names. They are unusual (you increase the WTF/LOC measurement of your code which is never good).
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.