Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) Header file

 

Header files contain the declarations of functions and classes.

 

Header files commonly have the .h and .hpp filename extensions.

 

To use a header file, it must be #included in the source code.

 

#include <iostream>
#include "Widget.h"
 
int main()
{
  Widget w;
  std::cout << "Hello world" << std::endl;
}

 

A complete collection of header files is called a library. The C++ standard library is called the STL.

 

The combination of a header (.h) file and an implementation (.cpp) file is called a unit.

 

Make header files self-sufficient [1].

 

Always write interal #include guards [2]. Never write external #include guards [2] (but compare [8], which suggest to do write external #include guards).

 

 

 

 

 

What does and what does not belong in a header file

 

 

From [5]:

 

//Code copied from:
// * John Lakos. Large-Scale C++ Software Design. 1996. ISBN: 0-201-63362-0. Chapter 1.1.3, Figure 1-3, page 28.

//radio.h
#ifndef INCLUDED_RADIO
#define INCLUDED_RADIO

int z;                              // illegal: external data definition
extern int LENGTH = 10;             // illegal: external data definition
const int WIDTH = 5;                // avoid: constant data definition
static int y;                       // avoid: static data definition
static void func() {/*...*/}        // avoid: static function definition

class Radio
{
    static int s_count;             // fine: static member declaration
    static const double S_PI;       // fine: static const member declaration
    int d_size;                     // fine: member data definition
    // ...
  public:
    int size() const;               // fine: member function declaration
    // ...
};                                  // fine: class definition

inline int Radio::size() const
{
    return d_size;
}                                   // fine: inline function definition

int Radio::s_count;                 // illegal: static member definition

double Radio::S_PI = 3.14159265358; // illegal: static const member definition

int Radio::size() const { /*...*/ } // illegal: member function definition

#endif // INCLUDED_RADIO

 

 

 

 

 

 

Standard header files

 

The STL consists out of the following header files [3][4]:

 

  1. C++98C++11 algorithm
  2.  C++11 array
  3.  C++11 atomic
  4. C++98C++11 bitset
  5.  C++11 cassert
  6.  C++11 ccomplex
  7.  C++11 cctype
  8.  C++11 cerrno
  9.  C++11 cfenv
  10.  C++11 cfloat
  11.  C++11 chrono
  12.  C++11 cinttypes
  13.  C++11 ciso646
  14.  C++11 climits
  15.  C++11 clocale
  16.  C++11 cmath
  17.  C++11 codecvt
  18.  C++11 complex
  19.  C++11 condition_variable
  20.  C++11 csetjmp
  21.  C++11 csignal
  22.  C++11 cstdalign
  23.  C++11 cstdarg
  24.  C++11 cstdbool
  25.  C++11 cstddef
  26.  C++11 cstdint
  27.  C++11 cstdio
  28.  C++11 cstlib
  29.  C++11 cstring
  30.  C++11 ctime
  31. C++98C++11 complex
  32.  C++11 ctgmath
  33.  C++11 ctime
  34.  C++11 cuchar
  35.  C++11 cwchar
  36.  C++11 cwctype
  37. C++98C++11 deque
  38. C++98C++11 exception
  39.  C++11 forward_list
  40. C++98C++11 fstream
  41. C++98C++11 functional
  42.  C++11 future
  43.  C++11 initializer_list
  44. C++98C++11 iomanip
  45. C++98C++11 ios
  46. C++98C++11 iosfwd
  47. C++98C++11 iostream
  48. C++98C++11 istream
  49. C++98C++11 iterator
  50. C++98C++11 limits
  51. C++98C++11 list
  52. C++98C++11 locale
  53. C++98C++11 map
  54. C++98C++11 memory
  55.  C++11 mutex
  56. C++98C++11 new
  57. C++98C++11 numeric
  58. C++98C++11 ostream
  59. C++98C++11 queue
  60.  C++11 random
  61.  C++11 ratio
  62.  C++11 regex
  63. C++98C++11 set
  64. C++98C++11 sstream
  65. C++98C++11 stack
  66. C++98C++11 stdexcept
  67. C++98C++11 streambuf
  68. C++98C++11 string
  69. C++98C++11 strstream
  70.  C++11 system_error
  71.  C++11 thread
  72.  C++11 tuple
  73.  C++11 typeindex
  74. C++98C++11 typeinfo
  75.  C++11 type_traits
  76. C++98C++11 utility
  77. C++98C++11 valarray
  78. C++98C++11 vector
  79.  C++11 unordered_map
  80.  C++11 unordered_set

 

 

 

 

 

References

 

  1. Herb Sutter, Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. 2005. ISBN: 0-32-111358-6. Item 23: 'Make header files self-sufficient'.
  2. Herb Sutter, Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. 2005. ISBN: 0-32-111358-6. Item 24: 'Always write interal #include guards. Never write external #include guards'.
  3. International C++ Standard, table 11
  4. Draft of C++0x Standard, table 14 and 15, ISO/IEC JTC1 SC22 WG21 N 3290, Date: 2011-04-11, ISO/IEC FDIS 14882, ISO/IEC JTC1 SC22
  5. John Lakos. Large-Scale C++ Software Design. 1996. ISBN: 0-201-63362-0. Chapter 1.1.3, figure 1-3, page 28
  6. John Lakos. Large-Scale C++ Software Design. 1996. ISBN: 0-201-63362-0. Section D.2: Major Design Rules, Chapter 2, page 820: 'Avoid free functions (except operator functions) at file scope in .h files'
  7. Bjarne Stroustrup. The C++ Programming Language (3rd edition). 1997. ISBN: 0-201-88954-4. Section 9.5, item 4: 'Avoid non-inline function definitions in headers'
  8. John Lakos. Large-Scale C++ Software Design. 1996. ISBN: 0-201-63362-0. Section 2.5, page 85: 'Place a redundant (external) include guard around each preprocessor include directive in every header file'
  9. Joint Strike Fighter Air Vehicle C++ Coding Standards for the System Development and Demonstration Program. Document Number 2RDU00001 Rev C. December 2005. AV Rule 39: 'Header files (*.h) will not contain non-const variable definitions or function definitions.'

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict