You do not know C ++ namespaces

We are using every day using namespace std;, but do you know about namespaces?

What is that?

Namespace is a mechanism used to program a large number of logical link entities present in the composition together in a single identifier, and this identifier as a name of this group.
Put another way, is a collection of namespace whose key is the name

using namespace name
{
	/* code */
} ;// namespace name

Several related concepts (three successive decreases scopes)

  • Statement region identifier: Domain Declaration (declaration region)
  • Potential scope (potential scope): starting from the point of declaration, the area declared end of the field. (Because the principle is to use the C ++ statement, so before the point of declaration declaration space,Identifier can not be used
  • Scope (scope): an identifier that is visible to your program's scope, the range that is actually in use
    CAUTION : MS of MFC (Microsoft Foundation Class Library) does not use a namespace, but there are a lot using .NET

Defining namespaces

You can define a namespace famous and anonymous (as you define a structure as struct)
combined with a small example:

namespace Outer
{
	int i;										// 命名空间Outer的成员i的内部定义

	namespace Inner								// 子命名空间Inner的内部定义
	{
		void Fun1() { i++; }					// i为 Outer::i
		int i;
		void Fun2() { i++; }					// Inner::i
		void Fun3();
		} // namespace Inner
		void Fun1();
		// namespace Inner2;					// 错误,不能 声明 (而非定义)子命名空间
} // namespace Outer

void Outer::Fun1()
{
	i--;
}

void Outer::Inner::Fun3()
{
	i--;
}
  • You can not be defined in a namespacestatement(Another nested) sub-namespace onlydefinitionSubnamespaces
  • Namespace must be defined in the header file, so remember to create a .h file oh
  • Namespace are open, which means that any time a new name is added to an existing member of the namespace to go, but the rules Well, that is not used directly 命名空间名::成员名in the form, and must be declared in the definition of a namespace, The method is repeated declarations and definitions for the same namespace, every time you add a new member
  • It may also be a combination of the existing namespace (using the using)
    the For Example:
namespace sample
{
	int i;
	void Fun1();
}												// 现在有成员i, Fun1()
namespace sample								// 重新定义命名空间sample
{
	int j;
	void Fun2()
}												// 现在有成员i, Fun1(), j, Fun2()
namespace sample1
{
	using namespace sample;
	void Fun3();
}												// 组合命名空间

use it!

  • Use the scope operator "::" (just like you did the class members do)
  • Using directive
    • Standard C ++ library (not including the standard C library) all matter contained (including constants, variables, structures, classes and functions, etc.) are defined in a namespace std (standard criteria), but the pre-compiler ( #include) section should containWithout the .hStandard header files (e.g. #include <iostream>) thenusing namespace std;
    • using custom namespace is:
      using namespace 命名空间名[ ::子命名空间名1…… ]
      When there is only one word space, can be omitted to write []
      asusing namespace Outer::Inner;
    • Write sub-namespace instead of one (do not / can not write additional) based namespace, or is likely to cause a name conflict
    • Like the rest of the class to use as
  • Using declaration
    • using 命名空间名::[子命名空间名::子子……]成员名;
    • Use the equivalent of eliminating the need for a single member to write every time trouble scope operators, but the other members in the namespace still have to rely on scoping operator to locate
  • Comparison of using instructions and using statement
    • After using commands to use, once and for all, its entire namespace all members are valid; but using declaration has to be declared separately for different members
    • But in general, using the statement will be more secure. Since using declaration will only import the specified name, if the name change and the local name conflict, the compiler will complain. And using directive imports, including those never use the name, and if one has a name and a local name conflict, the compiler will not issue any warning information, but only covers members of the same name in the namespace with a local name
    • Conclusion: Although the use of namespaces There are several ways to choose from, but not just for the sake of convenience blindly using directive, as it is a departure from the original intention of the design namespaces, and have lost the function namespace name of this should prevent conflict
Released four original articles · won praise 3 · Views 171

Guess you like

Origin blog.csdn.net/weixin_45494811/article/details/104031808