+ 13

Include header files which are already included in other header files - Is it necessary?

Hai guys, I have a question here. So I have main.cpp, Linked_list.h and design.h. I already have <iostream> header included into design.h, and my Linked_list.h file includes design.h. If I now include Linked_list.h into main.cpp, do I need to include <iostream> for main.cpp as well? As far as I am concerned, my program is working fine without including <iostream> into main.cpp. TIA

7th Apr 2017, 12:21 PM
Hatsy Rei
Hatsy Rei - avatar
5 Respostas
+ 8
Works...vs. future-proofing. Philosophy: "Include what you use" http://softwareengineering.stackexchange.com/a/262020 "...always include all headers...used in a .cpp file...regardless of what you know about what's in those files. [Include guards ensure multiple includes don't matter]. [This strategy produces 'clarity' and 'refactor safety']" Include guards are standard practice; compilers even look for them: https://gcc.gnu.org/onlinedocs/cppinternals/Guard-Macros.html Guards prevent reopening the file and the practice is called "multiple include optimization". Google Code: https://github.com/include-what-you-use/include-what-you-use/blob/master/README.md "When every file includes what it uses, then it is possible to edit any file and remove unused headers, without fear of accidentally breaking the upwards dependencies of that file. It also becomes easy to automatically track and update dependencies in the source code."
8th Apr 2017, 12:05 AM
Kirk Schafer
Kirk Schafer - avatar
+ 6
The header defines functions. So defining the header in another, seems like one folder inside another.
11th Apr 2017, 3:59 AM
Manual
Manual - avatar
+ 2
Yes, you dont need to double include headers if its already declared in another header that you are using. But its easier to keep track of things if you stick to forward declaring and only declaring headers in cpp files unless you need to do it in a header
7th Apr 2017, 2:03 PM
aklex
aklex - avatar
+ 1
I had only just noticed my programs using the string class were compiling without including the <string> header. It turns out that <iostream>includes <ios_base> which in turn includes <string>. Is this bad practice and should I explicitly include <string>? Even if it's just a case of clarity? Is it safe to assume this applies to more than just the <string> header? Perhaps this is implementation specific and or does the standard state the <string> header be included via <ios_base> and <iostream>? Ensuring that any respected and widely used implementation will always include <string> providing the the call to <iostream> exists.
8th Apr 2017, 6:44 AM
Kuheli Patra
Kuheli Patra - avatar
0
I think I finally have an answer for this. In a header file, you have ifndef //and endif ifndef only allows you to define something that has not been defined already. If it has been defined already, the code defined inside is skiped and goes straight to endif. C++ inherited this from C to prevent double defined errors from header files.
25th May 2017, 1:01 AM
Manual
Manual - avatar