Connecting Tech Pros Worldwide Forums | Help | Site Map

#include efficiency question

Newbie
 
Join Date: Apr 2007
Posts: 1
#1: Apr 9 '07
I use XP Pro SP2, VC++ 6.0 SP6.
I am using <string> as an example:

I have a base class that uses <string>
Also have a derived class that uses <string>
main() also uses <string>

Should I #include <string> in every file that uses it? or should i let it go through the structured hierarchy, which poses my problem below:

main has access to many of the functions from included files that are included in my classes, but main needs no #includes to have this access.

This makes me wonder if I should #include in every file to show the reader, and myself, exactly what main is supposed to be using, rather than just blindly having access. But, is this innefficient in any way? to #include <string> when it already has access?

So, while ensuring maximum efficiency, where should I #include <string> ?

Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,169
#2: Apr 9 '07

re: #include efficiency question


Quote:

Originally Posted by Xorior

I use XP Pro SP2, VC++ 6.0 SP6.
I am using <string> as an example:

I have a base class that uses <string>
Also have a derived class that uses <string>
main() also uses <string>

Should I #include <string> in every file that uses it? or should i let it go through the structured hierarchy, which poses my problem below:

main has access to many of the functions from included files that are included in my classes, but main needs no #includes to have this access.

This makes me wonder if I should #include in every file to show the reader, and myself, exactly what main is supposed to be using, rather than just blindly having access. But, is this innefficient in any way? to #include <string> when it already has access?

So, while ensuring maximum efficiency, where should I #include <string> ?

This is a very good question. Lets address the question of efficiency first.

#including <string> multiple times is not going to effect the efficiency of the resulting program at all. It may slow down compilation a little but probably not noticeably as <string> is generally not a very large file (unlike windows.h say).

So this is a readability question only really, and thus down to judgement alone. You may have some coding standards that effect what you do, for instance I have worked on projects where the coding standards forbade including a header file from another header file, you where only allowed to include headers from C or C++ files. I have also worked on projects where all the headers were included into a single other header file which was then included into the C and C++ files. Both have advantages and disadvantages related to compile time and readability and working out what headers are included.

I can not tell you what to do on this, you should do what you feel comfortable with (or what you coding standards say). Whatever you choose to do you should stick to it consistently.
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,366
#3: Apr 9 '07

re: #include efficiency question


Using your string header as an example, be clear that you must

#include <string>

once in every file that needs it. This is because each file is separately compiled.

Should you include a header file twice in the same file, you may get redefiniton errors in the compiling of your program. To avoid this, headers like string, have an "inclusion guard"

#ifndef _STRING_
#define _STRING_

....contents of the header file here...

#endif

This preprocessor coinditional prevernts the contents of the header file from being included twice in the same source file.

Therefore, it matters not how many times you include a header file in the same source, the contents of the file will be included only once.
Reply