Thread: .cpp and .h

  1. #16
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    I think it's MSVC specific.

    How about just randomly spam some letters on the keyboard . Or use a pseudo random number generator while you are at it.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    But then what happens if you have two include files with the same name in different directories?
    The obvious solution that keeps in line with dwks' suggestion is DIRECTORY_HEADER_H. I have used NAMESPACE_CLASS_H, though I suppose FILENAME_H_MONTH_DAY_YEAR works well too.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Is there anything wrong with just using a random number? I actually wasn't joking .

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Is there anything wrong with just using a random number?
    I do not think so, other than that it could be troublesome since the probability of having duplicate names is rather small anyway, especially when things like namespace names, directory names, or the current date is added in, and these can come to mind immediately.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I think she means prefix.

    For example, I call my files PROJECT_PATH_TO_FILENAME_SR_H

    The SR_H suffix is personal for me and should guarantee that my guards never collide with those of other people (unless they have the initials S.R. too).

    #ifndef IOCHAIN_INFRASTRUCTURE_CHAIN_HPP_SR_H
    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. #21
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Perhaps you mean creation date, cpjust? It didn't occur to me right away, but I guess that's one way of doing it.
    I was thinking of some directory directive, like the way OS separates files.
    C:\foo\x.h
    and
    c:\x.h
    wouldn't be the same.
    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.

  7. #22
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    Perhaps you mean creation date, cpjust? It didn't occur to me right away, but I guess that's one way of doing it.
    I was thinking of some directory directive, like the way OS separates files.
    C:\foo\x.h
    and
    c:\x.h
    wouldn't be the same.
    Yes, the MONTH_DAY_YEAR part is the date when you first create the header file. You could also throw in the HOUR_MIN_SECOND if you really want, but I don't usually create that many header files on the same day.

  8. #23
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    I generally use COMPANYINITIALS_HEADERNAME, since it is unlikely that it will collide with a header of someone else at the company while im here, and what happens after isnt my responsibility.

  9. #24
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by cpjust View Post
    Yes, the MONTH_DAY_YEAR part is the date when you first create the header file. You could also throw in the HOUR_MIN_SECOND if you really want, but I don't usually create that many header files on the same day.
    I do, in the initial phase of development. That's when I'm writing the rough interfaces, and that means many, many headers created in a single day.
    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

  10. #25
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    In a project that made use of xuni, I accidentally created a header file called "graphics.h" -- which xuni also has. Both were using GRAPHICS_H for the inclusion guard. So now all of xuni's header files have guards like XUNI_GUARD_GRAPHICS_H.

    It took me quite a while to figure out what was wrong. Inclusion guards aren't supposed to fail. So I can see why someone might want to put a random number or other unique string inside them . . . .
    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.

  11. #26
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Or simply use #pragma once. Though not standard, according to wikipedia it's "widely supported", "without possibility for name clashes" and "increased compilation speed".
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  12. #27
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Yes, apparently even GCC supports it, though it's "deprecated". (I'm assuming MSVC does too; it likes #pragmas.)

    On the other hand, apparently it doesn't always work, while inclusion guards do.
    http://groups.google.com/group/comp....279780c364628f
    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.

  13. #28
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Microsoft's #pragma once doesn't help with collisions, however. I found out that the hard way.
    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.

  14. #29
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    So what is it good for then? Does it "speed up compile times" or something?
    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.

  15. #30
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    According to microsoft's documentation, it makes the compiler open each file once, speeding up compile times.
    Quote Originally Posted by MSDN
    Specifies that the file will be included (opened) only once by the compiler when compiling a source code file.

    Remarks
    This can reduce build times as the compiler will not open and read the file after the first #include of the module.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Get .cpp and .h files to communicate...
    By yaya in forum C++ Programming
    Replies: 6
    Last Post: 11-25-2008, 12:45 AM
  2. .cpp and .h files - organization
    By ChadJohnson in forum C++ Programming
    Replies: 4
    Last Post: 01-12-2006, 11:40 PM
  3. Why Declare in .H but Define in .CPP ?
    By Krak in forum C++ Programming
    Replies: 10
    Last Post: 07-30-2005, 12:36 AM
  4. placement of (.h) and (.cpp) files..!?
    By matheo917 in forum C++ Programming
    Replies: 3
    Last Post: 02-22-2003, 06:37 PM
  5. Class files (.h and .cpp
    By Todd in forum C++ Programming
    Replies: 7
    Last Post: 02-14-2002, 03:07 PM