Thread: Including default headerfiles in custom headerfiles

  1. #1
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719

    Including default headerfiles in custom headerfiles

    Ok, So I've just got the hang of working with header files, and source files together. I am making a text game, to better learn how header files work with the source files. I made a header file called race.h for the different races. And in this headerfile is the class race. In the class I have a bunch of int variables for max health,stamina, etc. I also want to have a string or array declared in this class. But is it "ok" to include <string> in my custom header file? I am doing this because it will declare a name variable. Ex. string NAME;. So is it ok to do this?

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    What is a "default" header file?

    I also want to have a string or array declared in this class. But is it "ok" to include <string> in my custom header file?
    If you declare a string type in the file, you have to include <string>. The string type is defined in the string header file, so the only way the compiler knows what "string" means is if you include the <string> header file.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> So is it ok to do this?

    Yes. However, the convention is to not use the using directive in header files, so it would be std::string NAME; (or even better std::string name; since upper case names are usually constants by convention).

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    It's fine to include system headers (or any kind of header, really) in headers. It's considered bad practice to use using statements or declarations (e.g. using namespace std; using std::cout; ) in headers, because it could influence code that includes them.
    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

  5. #5
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Ok, that's cool. What I mean by default header file, are header files such as <iostream> , <cstdlib>, <string>. I didn't have any other word to describe them. Just wanted to make sure I can use those header files in MY headerfile. Thanks.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    They're usually called standard headers. Then there are system headers such as <windows.h> (usually only available on a specific system), library headers like, for example, <zlib.h> (depend on a library being correctly installed), and finally your own headers.
    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

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Just wanted to make sure I can use those header files in MY headerfile. Thanks.
    What you need to avoid doing though is using the namespace, like this

    Code:
    #include <string>
    using namespace std;
    class foo {
      string mString;
    };
    This is very bad, everything which includes the header file will get the whole of the standard namespace whether it likes it or not.


    It's much better to fully specify what you want, like this
    Code:
    #include <string>
    class foo {
      std::string mString;
    };
    Each file which includes this header can then import as much or as little of the standard namespace as it wants to.

  8. #8
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Yeah, someone mentioned that before. I wasn't going to use it anyway, I would just use std:: if I needed something that used the namespace.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Utilizing another compiled program for a task.
    By kotoroshinoto in forum C Programming
    Replies: 6
    Last Post: 06-03-2008, 01:43 PM
  2. Including -lm as default for compilling in ajunta
    By GuilhermeBrant in forum C Programming
    Replies: 2
    Last Post: 04-06-2008, 07:57 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. Including a custom class file
    By Beowolf in forum C++ Programming
    Replies: 29
    Last Post: 09-29-2007, 05:29 PM
  5. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM