Difference between revisions of "Cpp Coding Standards/HEADERS"
From Apache OpenOffice Wiki
Line 4: | Line 4: | ||
---- | ---- | ||
=== Summary === | === Summary === | ||
− | |||
− | |||
− | + | ==== Self Sufficient and Minimal <span id="Self">(Self)</span> ==== | |
+ | |||
+ | When included a header file should be self sufficient and minimal. | ||
+ | * Include definitions for superclasses and data members | ||
+ | * Create forward declarations for everything else | ||
+ | * Include the definition as late as possible, this helps reduce physical dependencies | ||
+ | |||
+ | // Foo.hxx | ||
+ | // Superclass and data member | ||
+ | #include "Super.hxx" | ||
+ | #include "Data.hxx" | ||
+ | |||
+ | // Forward declarations | ||
+ | class Param; | ||
+ | |||
+ | // This does not need a class definition | ||
+ | Param doSomething1(Param aParam); | ||
+ | |||
+ | class Foo : public Super | ||
+ | { | ||
+ | public: | ||
+ | // This does not need a class definition either | ||
+ | Param doSomething(Param aParam); | ||
+ | |||
+ | private: | ||
+ | Data mData; | ||
+ | }; | ||
+ | |||
+ | // Anotherfile.cxx | ||
+ | #include "Foo.hxx" | ||
+ | #include "Param.hxx" // Now we need the definition | ||
+ | |||
+ | ... | ||
+ | Param p; | ||
+ | p = doSomething1(p); // Error without the definition | ||
+ | |||
==== Include Directly <span id="IncDirect">(IncDirect)</span> ==== | ==== Include Directly <span id="IncDirect">(IncDirect)</span> ==== | ||
Include the header files for all types you need directly, not via another file. | Include the header files for all types you need directly, not via another file. | ||
− | + | ==== Precompiled headers <span id="IncPCH">(IncPCH)</span> ==== | |
+ | |||
+ | * The statement in each cxx files should be the inclusion of precompiled headers. There must be no other includes, no definitions and no include guards around the include statement. Forget this for a new file and you break the build. | ||
+ | * Assume the precompiled header file is empty and include all your headers normally. | ||
+ | |||
+ | #include "precompiled_foo.hxx" // May or may not contain includes for Foo.hxx and Bar.hxx | ||
+ | |||
+ | #include "Foo.hxx" | ||
+ | #include "Bar.hxx" | ||
+ | |||
---- | ---- | ||
[[Category:Coding Standards]] | [[Category:Coding Standards]] |
Revision as of 14:59, 15 December 2006
Topic-Id: HEADERS
What to do or do not with header files.
Contents
Summary
Self Sufficient and Minimal (Self)
When included a header file should be self sufficient and minimal.
- Include definitions for superclasses and data members
- Create forward declarations for everything else
- Include the definition as late as possible, this helps reduce physical dependencies
// Foo.hxx // Superclass and data member #include "Super.hxx" #include "Data.hxx" // Forward declarations class Param; // This does not need a class definition Param doSomething1(Param aParam); class Foo : public Super { public: // This does not need a class definition either Param doSomething(Param aParam); private: Data mData; };
// Anotherfile.cxx #include "Foo.hxx" #include "Param.hxx" // Now we need the definition ... Param p; p = doSomething1(p); // Error without the definition
Include Directly (IncDirect)
Include the header files for all types you need directly, not via another file.
Precompiled headers (IncPCH)
- The statement in each cxx files should be the inclusion of precompiled headers. There must be no other includes, no definitions and no include guards around the include statement. Forget this for a new file and you break the build.
- Assume the precompiled header file is empty and include all your headers normally.
#include "precompiled_foo.hxx" // May or may not contain includes for Foo.hxx and Bar.hxx #include "Foo.hxx" #include "Bar.hxx"