Cpp Coding Standards/HEADERS/Self

From Apache OpenOffice Wiki
Jump to: navigation, search

Self Sufficient and Minimal (Self)

When included, a header file should be self sufficient and minimal.

  • Include definitions for superclasses, data members, typedefs, macros, types denoted in exception-specifications, ...
  • Create forward declarations for everything else (but note that this introduces unwanted coupling, when for example a template declaration is later on extended with defaulted parameters, or a struct definition is replaced with a typedef)
  • 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
Personal tools