[Home]CppCodingStandards/CppFileConventions

TheSourcery | CppCodingStandards | RecentChanges | Preferences | Index | RSS

C++ File Conventions

File Names

File names should not contain underscores and should match class names if appropriate. File names should not be case sensitive and by default will be lower case. File names should not contain extraneous whitespace, or other special characters other than letters or digits. File names should only contain one period, to separate the file extension. C++ source files will use the .cpp extension. C source files will use the .c extension. All header files will use the .h extension

Example:

  workq.cpp
  workq.h 
  executor.c

Source File Layout

Source files should contain the following components in the order shown:

  1. Prolog
  2. System #includes
  3. Application #includes
  4. External functions
  5. External variables
  6. Constants
  7. Static variable initializations
  8. Public methods
  9. Protected methods
  10. Private methods
  11. Functions

Header File Layout

Header files should contain the following components in the order shown:

  1. File guard
  2. Prolog
  3. System #includes
  4. Application #includes
  5. #defines
  6. Macros
  7. External functions
  8. External variables
  9. Constants
  10. Typedefs
  11. Structs
  12. Forward declarations
  13. Class declarations
  14. Public methods
  15. Protected methods
  16. Private methods
  17. Inline method definitions
  18. Functions

Small inline methods like trivial accessors may be implemented in the class definition.

Header Guards

All header files should contain a file guard mechanism to prevent multiple inclusion. This mechanism is implemented as shown by the following lines:

  #ifndef FileName_H      // first line of the header file
  #define FileName_H      // second line of the header file
  ...                     // body of the header file
  #endif  // FileName_H   // last line of the header file; note comment


TheSourcery | CppCodingStandards | RecentChanges | Preferences | Index | RSS
Edit text of this page | View other revisions
Last edited September 13, 2005 9:35 pm by JonLambert (diff)
Search:
All material on this Wiki is the property of the contributing authors.
©2004-2006 by the contributing authors.
Ideas, requests, problems regarding this site? Send feedback.