Thread: Header files in .h

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688

    Header files in .h

    This is question rather than a problem.

    I know it is bad to place header files in class definition files, as this means once the header file is included into the .cpp file it will include all the headers in the .h fille that are not needed.

    I follow this rule to as much as I can, however, is it ok to do this:

    Code:
    #ifndef FOO_H
    #define FOO_H
    
    #include <string> // i need this!
    
    class
    {
    Foo();
    ~Foo();
    
    private:
        std::string m_MyString;
    };
    
    #endif
    If I compiled the code without the string header, I would get a compile error winging it string was not a member of std::. So, is this allowed in the sense of "a file should only get what it requries and nothing more"?
    Double Helix STL

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    That is fine, although another option (not recommended) is that any file which includes this particular header could also #include the string header before you #include this one. This can confuse people who simply move around the order of the #include statements and suddenly they start getting errors. So... what you are doing above is the way to do it.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I'm guessing STL headers are typically guarded by include guards (from what I can see anyway). Then you face no penalty at all by doing the above. In fact, I would recommend it.
    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.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    In my opinion, all include files should never require that other includes are made before this one.

    As a consequence: If one include file relies on any other header file (e.g. a class uses std::string), then it should include the header file(s) necessary for it to compile with only itself being included.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I know it is bad to place header files in class definition files, as this means once the header file is included into the .cpp file it will include all the headers in the .h fille that are not needed.
    This is nonsense. By the very act of including the header that requires the other headers, the cpp file requires those other headers. Dependency is a transitive relationship. (If A depends on B and B depends on C, A depends on C.)

    You should not include any more headers than strictly necessary, and you never should place using statements on global or namespace scope. (Avoid using declarations too, unless you actually mean to import a symbol into a target namespace.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If you need certain header files for types in the public section of a class, you should always include that header in the class definition.
    If you need certain header files only for types in the private section of a class, you can use the Pimpl idiom:
    Chapter 43. Pimpl judiciously

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. #include header files or .cpp files?
    By DoctorX in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2006, 12:21 PM
  2. Missing header files and libraries with Borland
    By quagsire in forum C++ Programming
    Replies: 5
    Last Post: 03-26-2003, 06:19 AM
  3. placement of (.h) and (.cpp) files..!?
    By matheo917 in forum C++ Programming
    Replies: 3
    Last Post: 02-22-2003, 06:37 PM
  4. Header files
    By Jez_Master in forum C++ Programming
    Replies: 2
    Last Post: 04-08-2002, 07:56 AM