Thread: Header file (multiple inclusions)

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    38

    Header file (multiple inclusions)

    I get the impression (from previous posts) that when working with large numbers of header files, the coder should always include header "guards" to keep them from being included more then once. Header guards being the ("ifndef HEADER_H ....stuff). This is something that I have not been doing, and I am constantly getting redefinition errors.
    Code:
    #ifndef __yourheader_h__
    #define __yourheader_h__
    
    //all your code goes here
    
    #endif
    A few questions about this:
    (1) Is this the best way to avoid having classes included multiple times (thus causing errors)?
    (2) Is this techinique "Good Coding Practice" or is it a "work around"...ie are you guru's doing this too?

    Formating questions:
    (1) if the header file that I am creating also needs to "#include" other classes, does it matter if I do that before the "#ifndef" guard statement or after?
    (2) Does the name that I used in the #ifndef _HEADERNAME_H matter (ie. do I have to use underscore, or can I just use the class name?)
    (3) I have also seen this technique (below) online...when/how (if ever) should I use this?
    Code:
    #ifndef _HEADERFILE_H
    #include "HeaderFile.h"
    #endif
    Thanks for your help
    Last edited by cjschw; 08-10-2004 at 09:54 AM.
    chris

  2. #2
    Registered User
    Join Date
    Jul 2004
    Posts
    101
    >>Is this the best way to avoid having classes included multiple times (thus causing errors)?
    No, but it is the least bad method C++ supports presently.

    >>ie are you guru's doing this too?
    I cannot speak for the gurus, but header guards are a common practice to avoid multiple inclusion across the board.

    >>does it matter if I do that before the "#ifndef" guard statement or after?
    Good design says that you should not include headers more than you need to, even if it does no damage. So any headers required should be placed inside of the header guards.

    >>Does the name that I used in the #ifndef _HEADERNAME_H matter
    Yes, it does. Do not begin the name with underscores as that invades the implementation namespace. Other than that you can use whatever you want as long as your names are not likely to clash with other names.

    >>I have also seen this technique (below) online...when/how (if ever) should I use this?
    If I recall correctly, that is simply another way of implementing header guards. The first example you posted is a much cleaner method in my opinion.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM