Recently learning C + +, the concept of namespace is rather curious, but the online information is very fragmented, and do not want to go through the thick C + + Primer Plus. So he experimented.
1. <iostream> and <iostream.h> are not the same, the former has no suffix, in fact, in your compiler include folder can be seen, the two are two files, open the file will find that the code is not the same.
The header file with the suffix. h C + + standard has been explicitly not supported, the earlier implementation of the standard library functions defined in the global space, declared in the header file with the. h suffix, C + + standard in order to distinguish with C, and in order to correctly use the namespace, the header file does not use the suffix. h.
Therefore, when using <iostream.h>, the equivalent of calling library functions in C, using the global namespace, which is the early C + + implementation;
When using <iostream>, the header file does not have a global namespace defined, and namespace STD must be used so that cout can be used correctly.
Above from http://www.jb51.net/article/40018.htm
The role of 2.namespace
Reduce the problem of variable functions and class names for large software. Each namespace can be arbitrarily named variable, do not worry about conflict;
namespace Space1{int x;} namespace Space2{int x;}
You can also add functions in namespace
namespace Space1{int x;} Namespace Space1{void alert () { std::cout<< "Hello";}}
3. How to invoke a member in Namaspace
Call all using namespace xxx at once;
Call some of these members
Xxx::member
Xxx::function ()
Like in the standard library.
std::cout<< "";
Or
using namespace std;cout<<"";
Scope of 4.namespace
#include <iostream>using namespace Std;int main () { int name = 8; cout << name; System ("Pause");}
Where the members of STD can be called globally. However, if the using line is inside main, the relevant content is not available in the external function definition.
Nesting of 5.namespace
such as the following definition
namespace layer1{ int x; namespace layer2{ int y; }}
How do I invoke the content in Layer2?
Using namespace layer1;using namespace Layer2;
If the order in turn will be an error;
or using
Layer1::layer2::x
method to make a partial call;
6. If the same variable is declared after namespace has been introduced, the newly declared variable is used.
#include <iostream>using namespace std;namespace test{ int name = 4;} int main () { using namespace test;
int name = 8; cout << name; System ("Pause");}
The program runs with a result of 8.
7. Unnamed namespace
namespace {int x;}
is equivalent to
static int x;
The default value is already initialized to 0.
namespace in C + +