Direct plus global scope:
namespace namespace mainly used to solve the problem of naming conflicts
namespce A
{
int m_b = 100;
}
1, can be put in the namespace functions, variables, structure, class
2, then the namespace must be defined global variables
namespace A
{
void func()
int m_a=20;
struct person
{
};
class Animal{};
namespace B
{
int m_a = 10;
}
}
3, can be nested namespaces
namespace A
{
namespace B
{
int m_a = 10;
}
}
4, the namespace is open, you can go at any time of the original namespace additions to this A namespace will be merged and above namespace A
namespace A
{
int m_a=0; //相当于static int m_c,只能在当前文件夹使用
}
namespace A
{
int m_a = 10;
}
5, nameless, anonymous namespace
namespace
{
int m_c=0; //相当于static int m_c,只能在当前文件夹使用
}
6, can play a namespace alias
namespace veryLongName
{
int m_c=0; //相当于static int m_c,只能在当前文件夹使用
}
void test()
{
namespace short = veryLongName;
}
First look at a problem
output is 20, and add a statement after the error has occurred!
Compiler directives