2 general questions

Im not new to c++ but am still a beginner, i've got 2 questions and they could just be personal preference but need to ask.. while in school we learned by creating empty projects to do our coding, but am doing some pluralsight training to brush up on my skills and the person doing the lessons uses precompiled headers. Is there any benefit to either or if i were making something proffesional would it be better to use the precompiled or an empty project? here is the second question. I also had learned before to always include namespace std; and like before. the lessons im watching at the moment does not use the include namespace instead just does the std::cout << "something" << std::endl; style.. it would seem easier and less coding if you were to include the namespace ,but curious if there's a benefit of one over the other.
Thanks
For the second question: when you use the std:: prefix (or another namespace), it is easier to see which objects are in which namespace: it makes the code easier to read.
1)

Normally, header files are compiled for each source file they are included in. If you have very large headers that are included in several different cpp files, this can cause compilation to take longer. Precompiled headers potentially speed this up by compiling the header only once, then using that compiled data in every source file which includes that header.

Personally, I have had some problems with flakiness of pch files (where they don't always get refreshed/recompiled when they should be)... and those issues outweigh the benefits. So I avoid using them unless they provide a significant reduction in compile time.

Neither approach is more "professional". Some professional projects use pch's, others don't. it depends on the project/team/etc.


2)

Namespaces are meant to avoid name conflicts. The idea is that everything in the standard library is put in the std namespace so that the names of its functions/classes/etc don't get in the way of the names of stuff in your code. By putting using namespace std; you are dumping the std namespace into the global namespace, effectively defeating the entire point of using a namespace in the first place.

closed account (o3hC5Di1)
Hi there,

By precompiled headers I assume you mean libraries such as <string> ?
These are sometimes necessary for you to use the functionalities described in those headers, such as with <string> if you want to use C++ std::string objects.

If you mean creating your own header files in multifile programs, that's mostly done to keep the code structured and easily retreivable.

As for your question on using namespace std; have a look at these threads:

http://www.cplusplus.com/forum/beginner/14325/
http://www.cplusplus.com/forum/general/72248/
http://www.cplusplus.com/forum/beginner/60663/
http://www.cplusplus.com/forum/beginner/49748/

Hope that helps.

All the best,
NwN

Edit: Wow, I really need more practice typing...
Last edited on
Topic archived. No new replies allowed.