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.