c ++ namespace (this one is enough)

1. Why do you need a namespace?

 using namespace std;

This sentence we almost always see in c ++ program, which is to use the namespace std.

ANSI C ++ namespace is introduced by a user named scope, procedures for handling common naming conflicts.
Defines three levels of the role of the city in C: file (compilation unit) , function and compound statements , C ++ and the introduction of the class scope , class is present within the file. You can define a variable with the same name in different scopes. Interfere with each other, to facilitate the system to distinguish them.
For example scopes:
If two classes are defined in the document, this function can have two classes with the same name, in reference, in order to distinguish, should be added as the class name is defined as:

class A //声明A类
{	
public:
	void funl ();//声明A类中的funl函数
private:
	int i;
);
vold A::funl ()//定义A类中的fun1函数

{
	//
}


class B //声明B类
{public:
	void funl () ;//B类中也有funl函数
	void fun2 () ;
};
void B: :funl ()//定义B类中的funl函数
{
	//
}

So it will not be confused.

Files can be defined in a global variable, its scope is the entire program. But if you define a global variable in a document A:

int  a=1;

Also defines a global variable in a file B:

int a=2;

In the document A and B separately compiled when there is no problem, but a program that contains these two files, when linking'll get an error because there are two variables of the same name in the same program, considered a repeat of the variables definition. The problem is that the entire scope of the global variables in the program, in the same scope or should not have to do more than two entities on the same name, including variables, functions, and the like.
Although extern variable declarations by the same name in the same program to the same variables. If the following statement in the file B:

extern int a;

B indicates that the file is in a variable external variables defined in other files, thus the statement, a compile-time variable scope A file on the file extension to B, there would be no run-time error, but in a there are several large-scale program to complete individual, different people define the classes in different header files, if required classes in the main file, it is possible to use the same name in a different header file naming classes and functions, naming conflicts which would appear in the program.
This obviously needs to propose ways to solve this problem, so only the namespace concept.

2. What is the name space

The so-called namespace, in fact, a programmer named memory space, the designer can specify some domain name space as needed, to some global entities were placed in each namespace, so that physical separation from other global open .
Such as:

namespace ns1  //指定命名空间ns1
{
	int a;
	int fun1();
}

Description : namespace keyword namespace is defined to be written, ns1 is the name of the specified namespace defined by the user, curly braces is the declaration block, the entity declared namespace become a member of, a, fun1, any course is a global variable, just hidden in the namespace. To use them in the program, it must be added to distinguish the namespace name and the scope operator "::", as ns1 :: a, which is called a namespace is defined. C ++ namespace role is similar to the relationship between directories and files in the operating system, because a lot of documents, inconvenience management, and easy to duplicate names, so people establish a number of subdirectories, files are put into different subdirectories, different sub files in the directory can be the same name. Should call the document states that the file path.
The role of the namespace is separated from each other to establish some scope, some separated by a global entity, in order to avoid name conflicts . For example, a middle school grade three have three students named Liu Fei, if in the same class, the teacher named Liu Fei points, three people stood up response, which is the name of the conflict, can not be distinguished from each other by the same name. To avoid overlapping slip mixed, put them into three groups, according to the group numbers as people, a group of Liu Fei, Liu Fei two groups, so as not to confuse the.

3. Use of namespaces

General usage rules: namespace name :: namespace Member Name

Sure enough, because "lazy", only in science and technology has been progress, but also the name of the namespace longer, again a nest, you will feel less convenient, to this end, c ++ provides mechanisms to simplify procedures for using namespaces .
3.1 namespace alias
from a namespace alias, such as:

namespace  TTTTTTTTT
{
//
}
//起一个短名字替代它
namespace T=TTTTTTTTT;     //别名T与TTTTTTTTT等价

3.2 namespace using a single member name
using the namespace must be added behind the name must be a member of the namespace qualified name. Such as:

using ns1::Students;

The above statement Statement: scope (scopes using where) will be used in the namespace ns1 a member, after a while using this scope, you do not always add namespace qualified.
For example: With the above statement:

Students std1(110,"liufei",22);//此处的Students相当于 ns1::Students

Without statement is this:

ns1::Students std1(110,"liufei",22);

3.3 "using namespace namespace name"
If a namespace member entities have more than one namespace, you need to repeatedly use the "using namespace name a single member," but you can use the namespace name using namespace statement to declare a namespace of all members. Such as:

using namespace ns1;

After such use, in the namespace ns1 it is like a member of the global domain statement as to define a space with a name no longer use.
3.4 unnamed namespace
document A statement following unnamed namespace:

namespace
{
	void fun()
	{
		cout <<"hello"<<endl;
	}
}

Since there is no name, apparently unable references in other documents, he only valid in this document A, where A is the file scope,
can think of static functions in C language statement, and its role is limited to the scope of the function this document.

3.5 standard namespace std (standard)
all identifiers standard c ++ library is in a namespace called std defined in, or standard header files (such as iosteram) in functions, classes, objects and classes are templates defined in the namespace std.
In this way, when the program you want to use c ++ standard library, you need to use std as limited, there is a sentence begins with the statement that we are talking about:
a using namespace std;
so that all identifiers are defined and declared in the std file can be used as a global variable.

Early use of 3.6 C library
used in c ++ header files in two ways
1. In a conventional method of C language
header file name suffix .h, such as stdio.h, math.h, etc.
2. With c ++ of new method
c ++ standard header files no longer contain the suffix .h, e.g. iostream, string are c ++ header file name, in order to express contact distinction c and c ++ a, c headers c ++ used without the suffix, and preceded by a letter c as c is stdio.h, in c ++ header file is the corresponding cstdio.

Published 29 original articles · won praise 8 · views 1993

Guess you like

Origin blog.csdn.net/qq_44785014/article/details/104271974