vividexstance's Profile User Rating: *****

Reputation: 651 Enlightened
Group:
Expert
Active Posts:
2,235 (1.71 per day)
Joined:
31-December 10
Profile Views:
10,863
Last Active:
User is offline Yesterday, 08:21 AM
Currently:
Offline

Previous Fields

Country:
Who Cares
OS Preference:
Who Cares
Favorite Browser:
Who Cares
Favorite Processor:
Who Cares
Favorite Gaming Platform:
Who Cares
Your Car:
Who Cares
Dream Kudos:
0
Expert In:
C/C++

Latest Visitors

Icon   vividexstance has not set their status

Posts I've Made

  1. In Topic: Infinite loop if enter a non-numberic character

    Posted 29 Jul 2014

    View Postdave3344, on 29 July 2014 - 02:13 PM, said:

    Maybe i get the wrong idea to check numeric checking?
    do
    2	{
    3	cout<<"Min of the secret number : ";
    4	cin>>min;
    5	}while(cin>>min);
    

    from this code, did ask the user to input the min two times?
    i thought i doing checking whether it is a numeric character or not

    If you notice in that code snippet, you can see cin >> min twice, once inside the loop's body, and the other in the condition for the do-while loop. This inputs into min two separate times.
  2. In Topic: Two basic questions that I need help with in dumbed down language

    Posted 28 Jul 2014

    View Postjimblumberg, on 28 July 2014 - 12:11 PM, said:

    Quote

    That's not entirely true. If you put "include guards" in your headers, and you should, then the header will only be included once. Also, if all the code is in the header file, then that means there is less compilation. If the code is in the .cpp (implementation file), then any changes to the code require a recompilation of that cpp file. But if the code is in the header, the only recompilation that is needed is for whatever code uses that header (and only if it uses the "new" code).


    This is not entirely true either. Header guards only prevent the multiple inclusion of the header into each compilation unit, and a single program can have multiple compilation units. Keeping all the code in the header can actually lead to more compilation.

    You normally gain at least a couple of things when you separate out the implementation code from the header. One it makes the header much easier to read, and the header should be the only thing the user of your "library" should need to view in order to use your "library", so you should keep the header as simple as possible. Second when you place the code in a separate source file and add this source file to your project this code will only be compiled once, no matter how many times you include the header.

    Quote

    But if the code is in the header, the only recompilation that is needed is for whatever code uses that header (and only if it uses the "new" code).

    If the code is in the header then every file that includes this header will need to be recompiled it the header changes. But in the case of the split system the only time all the files that include the header will need to be compiled is when the header changes. If only the implementation file changes only the implementation file needs to be compiled, saving compile time, and many times the header file is fairly static, most changes happen in the source file.


    Jim

    Please explain how code in a header file is compiled "more"? Let me give an example, you write a "library" and you have two options. Write the whole thing inside the header or split it up into the header/implementation. Now when you go to compile this, with the header-only implementation, you just compile the program that uses the header, of course the code in the header replaces the #include line and that's compiled. Or you have to compile the implementation file and the program file.

    Now maybe I'm mistaken about what exactly we're debating here. Are you trying to say the compilation takes longer with a header-only include file? Or just that the code using the header-only file needs to be recompiled more often?
  3. In Topic: Two basic questions that I need help with in dumbed down language

    Posted 28 Jul 2014

    View PostBungo Mungo, on 28 July 2014 - 11:57 AM, said:

    View Postvividexstance, on 28 July 2014 - 08:37 AM, said:

    Also, if all the code is in the header file, then that means there is less compilation. If the code is in the .cpp (implementation file), then any changes to the code require a recompilation of that cpp file. But if the code is in the header, the only recompilation that is needed is for whatever code uses that header (and only if it uses the "new" code).


    But what if that header file has includes to another file, and then that file has includes to 3 different files? Wouldn't that create a web of repeated compilations if it is included in multiple .cpp files? I'm still learning this as well so thank you for correcting any misinformation I provided.

    That's the point of the include guards. You have to realize that what you're talking about is being done by the preprocessor. So when you #include a header, the code in the header simply replaces the #include line to the code of header. If you have include guards, this is only done once, and not many times, even if the file is included many times.
  4. In Topic: C++ include, lib, bin

    Posted 28 Jul 2014

    Sounds like you're on linux, and it really depends on the library. Some libraries are precompiled and you just need to put the files in a "standard" location. Also, sometimes you have the source code to the library and you have to compile the library yourself. This is usually done with a ./configure && make && make install (might be sudo make install on your system). Really, there should be a README.txt or an INSTALL.txt which explains the whole process.

    I would also imagine that most systems come with OpenGL already installed.
  5. In Topic: Two basic questions that I need help with in dumbed down language

    Posted 28 Jul 2014

    View PostBungo Mungo, on 28 July 2014 - 11:30 AM, said:

    View PostNantucketSleighride, on 28 July 2014 - 07:47 AM, said:

    My first is - why use a .cpp file when I can include all of my code in the header?


    I have always learned that it is customary to only include declarations in header files and definitions in .cpp files. In basic terms, the header file declares what a class, function, method, etc. will do and the .cpp file defines how it is done. This standard also reduces compile time because if you have several .cpp files that include the same header file (or multiple header files), the compiler applies the pre-processor (combines the header file and the .cpp file) to every .cpp file one at a time. So if you have a ton of code in your header file and it is being processed multiple times, the compiler is doing a lot of duplicate work and the time taken increases.

    That's not entirely true. If you put "include guards" in your headers, and you should, then the header will only be included once. Also, if all the code is in the header file, then that means there is less compilation. If the code is in the .cpp (implementation file), then any changes to the code require a recompilation of that cpp file. But if the code is in the header, the only recompilation that is needed is for whatever code uses that header (and only if it uses the "new" code). Lastly, with templates in C++, its typical that all the code is placed in the header.

    An example of an include guard for "person.h":
    #ifndef PERSON_H
    #define PERSON_H
    
    // All code goes here
    
    #endif
    
    

    The macro doesn't have to be exactly the same as the file name, but it's good practice to have it be something like I did.

    *EDIT*:
    I forgot to add that another reason to split the declarations from the implementation is when you write a library or something similar and you want others to use it but you don't want them to know how its implemented. That way, they only have access to the declarations, which is all they need to be able to use the code.

My Information

Member Title:
D.I.C Lover
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:

Contact Information

E-mail:
Click here to e-mail me

Comments

Page 1 of 1
  1. Photo

    orihotomihsah Icon

    09 Sep 2013 - 09:45
    Hi
Page 1 of 1