[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppInclude
There are three different header file #includes:
The first, without the .h extension, means that this header file is from the Standard Template Library. The second #include means that the header is not from the STL but in the standard header file directory. The third #include means that the file is local, i.e. in the same directory as the program. If the local #include fails, the standard header file directory is checked for this header file.
This header file is a wrapper: all it does is call the C++ header file cstdio and then adds a 'using namespace std', as C does not have namespaces.
This has the unfortunate side-effect that after calling such a header file all functions and classes in namespace std will be in the global namespace.
As it pollutes the global namespace, avoid using namespace std [4,5].
An example from [6]:
Forgetting to add an implementation file to you project results in a linking error.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CppInclude
(C++) #include
A preprocessor statement to add a header file (.h or .hpp) or other files to your program.There are three different header file #includes:
- include <iostream> // 1: STL
- include <assert.hpp> // 2: The standard header file directory
- include "Unit1.h" // 3: Local
The first, without the .h extension, means that this header file is from the Standard Template Library. The second #include means that the header is not from the STL but in the standard header file directory. The third #include means that the file is local, i.e. in the same directory as the program. If the local #include fails, the standard header file directory is checked for this header file.
- Never #include unnecessary header files [1].
- Prefer to #include <iosfwd> when a forward declaration of a stream will suffice [2].
- Never #include a header when a forward declaration will suffice [3].
The C style include on ?CppStl STL headers
For backwards compatibility with C one can [[CppInclude- include]] C++ header files with the .h extension.
- include <stdio.h> //C-stle #include !!!
This header file is a wrapper: all it does is call the C++ header file cstdio and then adds a 'using namespace std', as C does not have namespaces.
This has the unfortunate side-effect that after calling such a header file all functions and classes in namespace std will be in the global namespace.
As it pollutes the global namespace, avoid using namespace std [4,5].
An example from [6]:
vector v1; //Error: no 'vector' in global namespace
- include <vector> //Carefully avoids polluting the global namespace
vector v2; //Oops: this now works
- include <stdio.h> //Contains a 'using namespace std'.
Implementation (.c or .cpp) files
These normally are not #included. Instead, these are added to your project. For example, in C++ Builder, select 'Project | Add to Project'.Forgetting to add an implementation file to you project results in a linking error.
'#include' links
References
- 1) Herb Sutter. Exceptional C++. 2000. ISBN: 0-201-61562-2. Item 26: 'Never #include unnecessary header files'.
- 2) Herb Sutter. Exceptional C++. 2000. ISBN: 0-201-61562-2. Item 26: 'Prefer to #include <iosfwd> when a forward declaration of a stream will suffice'.
- 3) Herb Sutter. Exceptional C++. 2000. ISBN: 0-201-61562-2. Item 26: 'Never #include a header when a forward declaration will suffice'.
- 4) C++ FAQ Lite: http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5 .Item 27.5: 'Should I use using namespace std in my code? Probably not.'
- 5) Bjarne Stroustrup. The C++ Programming Language (3rd edition). 1997. ISBN: 0-201-88954-4. Chapter C.14.15: 'Don't pollute the global namespace'
- 6) Bjarne Stroustrup. The C++ Programming Language (3rd edition). 1997. ISBN: 0-201-88954-4. Chapter 8.2.9.1: 'Namespaces and C'.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
