headers and including

This is a discussion on headers and including within the C++ Programming forums, part of the General Programming Boards category; Hello.. When I write my programs I always include only file's header in each .cpp.. Is this a right way ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    headers and including

    Hello..

    When I write my programs I always include only file's header in each .cpp.. Is this a right way to do it or should I include files in cpp file?

    For instance:

    test.cpp:

    Code:
    #include "test.h"
    
    ..
    test.h:

    Code:
    #ifndef _TEST_HEADER_
    #define _TEST_HEADER_
    
    #include <windows.h>
    #include <string>
    
    etc..
    #endif

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    This is the correct way to do it. Starting your macro name with an underscore is kind of unsafe though because those names are reserved to the compiler's implementation so they can already be used... I've never encountered that problem though.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    Normally I only put in the .h file what is necessary to include there if I need to include that file in other source files (stuff which wants to use what is in test.cpp).
    That way, I don't have to figure out all of the dependencies of test.h just to be able to use it.

    If for example I have
    Code:
    #ifndef _TEST_HEADER_
    #define _TEST_HEADER_
    
    #include <string>
    void myfunc ( std::string param );
    #endif
    Then I would only need <string> in there, and not windows.h.

    I shouldn't need to know (or care) that test.cpp needs windows.h, since it will include it if it needs to.
    I certainly don't want the excess baggage of windows.h just to be able to call a function in test.cpp


    Some people go to the other extreme of not having ANY include files in include files, and exposing the entire set of dependencies in every source file. This creates it's own maintenance problem of updating everywhere a file gets included should its dependencies change.

    It really depends how you value your traceability.

    So there you have it
    - include everything the .cpp also needs
    - include only what's necessary for the header itself
    - include nothing
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Including headers in a header file
    By tjpanda in forum C++ Programming
    Replies: 3
    Last Post: 09-22-2008, 08:48 AM
  2. including headers and forward declaration
    By l2u in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2007, 02:47 PM
  3. problems with including headers
    By l2u in forum C++ Programming
    Replies: 3
    Last Post: 07-23-2006, 08:06 AM
  4. including headers inside headers
    By kromozom in forum C++ Programming
    Replies: 5
    Last Post: 04-18-2005, 10:56 AM
  5. including headers
    By HybridM in forum C++ Programming
    Replies: 2
    Last Post: 05-18-2003, 08:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21