Thread: header files

  1. #1
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377

    Question header files

    I've got a question : Is it good to use :
    Code:
    #indef SOMETHING_H
    #deinfe SOMEHING_H
    ... put your class here ...
    #endif
    Since if you include the header two times you don't get an error. But it seems that with this kind of protection you can include header only once, and every time you try to do it again the compiler ignores it, which prevents tracking bugs.

    Isn't it better to include the headers properly without using that above, and if you make a mistake the compiler warns you and you correct it.

    So do you use that above. To me it seems stupid ? Can you explain me why i'm wrong ?
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You can avoid it, but you have to be VERY careful about the order in which you include files.

    Also, if foo.h depends on bar.h, then you have to manually include bar.h BEFORE foo.h in all the places foo.h is included.

    In a large project, this kind of restriction can result in pages of #include at the beginning of each file and a maintenance nightmare.
    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.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Isn't it better to include the headers properly without using that above.
    Using header include guards is proper, IMO. Imagine if <string> did not have include guards. If you use a string object as a data member in multiple classes, and then use both those classes in a source file, what do you include? I can't think of a "proper" way to do that without the string header using include guards.

    >> every time you try to do it again the compiler ignores it, which prevents tracking bugs.
    I don't see how a header include guard prevents tracking bugs in any way. It only prevents a header from being included twice in the same compilation unit. While duplicate includes can be unnecessary, they don't cause much of a problem in general.

    There's really no reason to avoid header guards.

  4. #4
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Quote Originally Posted by ElastoManiac
    Code:
    #deinfe
    Yeah, it would work if it was spelled right
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Not to mention ifdef and the same macro name . . .

    and every time you try to do it again the compiler ignores it,
    . . . true . . .
    which prevents tracking bugs.
    false -- it's already been included into the file. If it would generate an error, the first instance would generate an error too.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by ElastoManiac
    Since if you include the header two times you don't get an error. But it seems that with this kind of protection you can include header only once, and every time you try to do it again the compiler ignores it, which prevents tracking bugs.
    What kind of bug would it hide or prevent you from tracking?
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    ElastoManiac, check google for "C++ ODR headers guards". ODR stands for One Definition Rule.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

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. #include header files or .cpp files?
    By DoctorX in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2006, 12:21 PM
  3. classes and header files
    By Drake in forum C++ Programming
    Replies: 8
    Last Post: 11-30-2006, 07:12 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM