In my current project, which is starting to get large'ish, I have enforced a pretty strict #include policy:
Each
namespace has a Forward.h file which contains forward declarations for all classes in that namespace and a typedef for it's smart pointer:
#include <parent/Forward.h> // Let me access anything I need in the parent namespacenamespace parent { namespace child { class ClassName; typedef boost::shared_ptr< ClassName > class_name_ptr;} }
Each
header in the namespace then only includes Forward.h files. The trick is to use the typedef'ed pointer whenever I need a class:
#include "Forward.h"#include <other_namespace/Forward.h> // I need something from other_namespacenamespace parent { namespace child { class ClassName { public: ClassName(); ~ClassName(); // [...] private: other_namespace::class_in_other_namespace_ptr mCION; };} }
Then in the
source file I include whatever is needed to make the classes complete.
This works really well, and it solves just about all dependency problems. The only cases where a non-Forward.h header needs to be included in a header is if
a) The class inherits from another class.
b) A needed class is templated.
c) It contains small POD structs, typedef's or enums.
All cases can be solved with just a little care.