Thread: Header in header

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    69

    Header in header

    Hello, I'd like to know if it is considered bad practice to include a header (.h) in another header. Is it? Why?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    My opinion is like this.
    My personnal rule is simple :

    My headers are guarded (Like those from the library)
    I include a header when I need it. (Can be in a source or a header)
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I'd like to know if it is considered bad practice to include a header
    No - it is not...

    I prefer header to be complete - so I can include it in any c-file without including any other header before it...

    On the other hand - I prefer to make header dependencies - minimal, so If the forward declaration is enough - I use it rather to include the other header with the full type definition.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    My own preference is this:
    Include my own needed headers from my headers (if A.h needs B.h, include B.h).
    Any system headers (iostream, windows.h, dshow.h, etc, etc) are not included. The project that includes the header is expected to have put those headers inside its stdafx.h - precompiled header.
    Last edited by Elysia; 03-27-2008 at 01:46 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I think most people follow vart's usage. #include everything in your header that needs to be included so that it will compile no matter what. Prefer forward declarations over #include's when they will work.

    If you use precompiled headers, then of course common headers placed there don't need to be included.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Posts
    69
    Thanks for the super fast replies, guys. I'll follow vart's (and others') advice.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    No, not if the header file is directly using something declared in another header, otherwise, if it's only used in the implementation rather than the interface, the other headers should be included in the .cpp files.

    Code:
    #ifndef MYCLASS_H
    #define MYCLASS_H
    
    #include <string>
    
    class MyClass
    {
    public:
        MyClass( const std::string&  name );  // std::string is referenced here, so we need to include <string>
    ...
    };
    
    #endif  // MYCLASS_H
    Code:
    #include "MyClass.h"
    #include <algorighm>
    
    MyClass::MyClass( const std::string&  name )
    {
        // std::transform is only referenced in the .cpp file, so <algorithm> should be included here, not in the .h file.
        std::transform( name.begin(), name.end(), name.begin(), ToLowerFunc );
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM