What are the C++ coding and file organization guidelines you suggest for people who have to deal with lots of interdependent classes spread over several source and header files?
I have this situation in my project and solving class definition related errors crossing over several header files has become quite a headache.
Some general guidelines:
foo.cxx
, everything defined in there had better be declared in foo.h
.#include
s whenever possible. This allows you to break the cyclic header dependencies. Essentially, for cyclical dependencies across separate files, you want a file-dependency graph that looks something like this:
A.cxx
requires A.h
and B.h
B.cxx
requires A.h
and B.h
A.h
requires B.h
B.h
is independent (and forward-declares classes defined in A.h
)If your code is intended to be a library consumed by other developers, there are some additional steps that are important to take:
include/
and src/
subdirectories in my C or C++ projects, where include/
has all of my public headers, and src/
has all of my sources. and private headers.I'd recommend finding a copy of John Lakos' book Large-Scale C++ Software Design. It's a pretty hefty book, but if you just skim through some of his discussions on physical architecture, you'll learn a lot.