Probably A Stupid Question

This is a discussion on Probably A Stupid Question within the C++ Programming forums, part of the General Programming Boards category; Identifiers with a leading underscore followed by an upper case letter and identifiers with a double underscore anywhere are reserved ...

  1. #16
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Identifiers with a leading underscore followed by an upper case letter and identifiers with a double underscore anywhere are reserved for the implementation, so its best not to use them just in case the implementation is using them. Many people recommend never using leading underscores at all just to be consistent.

  2. #17
    *this
    Join Date
    Mar 2005
    Posts
    498
    Quote Originally Posted by Daved
    JoshR, you should never #include a cpp file. The only potential exception is when you work with templates, and even then there are other solutions.

    As Dae says, the proper way to do it is to add each cpp file to your project or makefile. Use Dae's example for the proper way to setup multiple source files (with one minor change - don't use leading underscores followed by an uppercase letter, just remove that leading underscore from the _MY_CLASS_H_ part).
    Why don't you include a cpp file? I've always been taught to. Plus doesnt the compiler just copy the code from the .cpp into the .h before adding that to the client?

  3. #18
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    No. Each cpp file is compiled separately into an object file. The object files are then linked together by the linker. If you include all the cpp files into a single source file, then every time you change the implementation of one of your classes you have to recompile the entire thing. One of the biggest reasons for separate header and source files is so that the compiler can compile code based on the interface presented in another header file without having to know how that interface is implemented. That way you can change the implementation of a single class, and only that source file needs to be recompiled and the object files re-linked.

    When you have thousands of source files this makes a huge difference. When you only have a couple it is still the right way to go.

    I believe that some people might have taught to include cpp files as a way of getting around adding each source file to a project or make file, but it is bad advice.

  4. #19
    *this
    Join Date
    Mar 2005
    Posts
    498
    Hmm thats really interesting, I dont usually make projects with my files so I guess I've never really thought of it.

  5. #20
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Do not include *.cpp files!

    It is a bad habit which violates the premise of separating the definitions from the declarations.

    Instead, include the headers in the source files. Then, make sure that you compile your code (without linking, creating an intermediate object file). Then, the object file will be linked. Dae and Daved already mentioned the easiest way to do this.

    *edit*
    Alright, I didn't see the second page. Sorry for the duplication here.
    Last edited by Zach L.; 07-08-2005 at 08:43 PM.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  6. #21
    *this
    Join Date
    Mar 2005
    Posts
    498
    And how do you create an "intermediate object file"? is it truely THAT bad? will it slow down the program or make it have incorrect results?

  7. #22
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    From what I can see, if you are only using 1 file as your Project, then only 1 object file is being created. Meaning its all in one big file.

    One point of object files is so that you can seperate large projects where maybe you only update 5/30 of the files, so only 5/30 of the object files have to be recompiled and therefor wont take 2 hours to compile.

    Another point is that it seems if you changed one of the .cpp files (thats included in the .h file), it will not update when you compile, because it skips the compiling process. The compiling process is skipped because it skips recreating the object file unless the .cpp file thats in the Project list has changed, which it may not, but one of the other 'somehow included' .cpp files could have. This would result in an object file not being as updated as one of the actual files, and therefor incorrect results.

    Another point is that when using an IDE is it makes the object file for the .cpp (and gives you errors) then goes to the next .cpp file, this means it can catch any errors from 1 of the many files before finishing the compiling process so you dont waste 1-60 minutes compiling one large object file to find your errors.

    Doing it should effect how the linking goes because the point of .h is for declarations.. in the compiling process it does not need to know that the value is, but that it exists, which is the point of the .h. I'm unsure but I also think it could result in problems using the extern keyword.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  8. #23
    *this
    Join Date
    Mar 2005
    Posts
    498
    Ok I see what you guys are saying, but in my current coding situations where I dont even create projects (unless coding win32 api) and instead link like 2-5 (small-medium) headers it doesn't take long to compile the whole thing. But thats a really good idea, thanks guys.

  9. #24
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    So how does one go about implementing a make file in MSVC++ 6.0?
    MSVC++ 6.0

  10. #25
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    It's easier to just use a project in VC++. Under the Project menu select Add To Project and use New... to create a new, empty file or use Files... to add existing files to the project. You can add your header and source files, and VC++ will know to compile the source files and just keep track of the header files.

    So every new file that you write you should add to your project. As long as one and only one of the source files has a main function, when you hit Build (F7) it will work.

  11. #26
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    . . . and of course put an #ifndef/#define/#endif in that .h . . .
    Yesterday I would have agreed with you. But today I read a C book and it said that #ifndef was not recommended and that you should use #if !defined() instead. However, I don't know why this is. Does anyone else?
    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.

  12. #27
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Apparently you can use a 'defined' operator with an #if, and not an #ifndef, so that could be why. However I dont see what use using 'defined' will gain you in C++. I can see how it could POSSIBLY be useful in C because you use many macros in C, but if you are only using macros for .h file data, then I doubt theres a 1/10000 situation possiblity that you would want to 'defined' in C++. What it does:

    Code:
    #if  defined (macro1)  || !defined (macro2) || defined (macro3)
    printf( "Hello!\n" );
    #endif
    See you generally dont decided, hell, if this .h file is defined lets not include this code (why even have this code then?).

    Unless.. well, wait, this would probably be really useful for OpenGL where you're programming for cross-platforms. You #define which platform you are compiling for, then have the .h files that have the code for either windows, linux, solaris, or mac os say #if defined listing all the other platforms with || operator, basicly saying "if any other platform is defined, then I am not" which would make it easier for keeping a project together, and easily changing for which platform you want to compile.

    Is that one of the reasons? and anyone know if theres more, for C++ (theres probably more for C than C++).
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  13. #28
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    . . . I dont see what use using 'defined' will gain you . . .
    Neither do I. Just something I read, and wondered about.
    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.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Question Probably
    By Kyrin in forum C Programming
    Replies: 2
    Last Post: 05-07-2006, 12:51 AM
  2. Replies: 7
    Last Post: 11-03-2005, 11:17 PM
  3. Stupid Question
    By digdug4life in forum C++ Programming
    Replies: 22
    Last Post: 05-17-2005, 11:43 AM
  4. stupid, stupid question
    By xelitex in forum C++ Programming
    Replies: 5
    Last Post: 12-22-2004, 07:22 PM
  5. Stupid question: What does Debugger do?
    By napkin111 in forum C++ Programming
    Replies: 6
    Last Post: 05-02-2002, 10:00 PM

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