Enclose definitions in header files

Enclose all header file definitions, and all the necessary antecedents, in a #ifndef construct. This saves you and your clients from having to figure out whether you have already included them.


      // MyClass.h
      #ifndef Taligent_MYCLASS
      
    
      #define Taligent_MYCLASS
      #ifndef Taligent_PREREQUISITE1
      #include "prerequisite1.h"
      #endif
      #ifndef Taligent_PREREQUISITE2
      #include "prerequisite2.h"
      #endif
      ... definitions for MyClass
      #endif
Now developers can include your header as many times as they want without errors. More importantly, you can include your header's prerequisites without caring whether they've already been included elsewhere (assuming that everyone follows this convention).

To speed up compilation, use the following construct in your files that include other files. (Don't use this for ANSI C or C++ header files because the symbols vary between compilers.):

      #ifndef MyCompany_FOO
      #include "Foo.h"
      #endif
This skips the overhead of reading and parsing Foo.h. This practice works especially well with symbol table load/dump; because the dump file defines the symbols, the include files need not be opened at all.


[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker