Scope and namespace in C++

4.1 Scope

Scope describes the scope of use of variables, constants, and functions in C++ .

There are generally several types of scopes:

1. Global scope

​ In the global scope, the functions or data we define are globally visible and can be called and used throughout the project. General declarations and definitions are outside the namespace. Generally, global variables need to be defined in the CPP file. If they are defined in the header file, the problem of repeated definitions will occur when they are introduced.

2. Namespace scope

​ Functions and data in the namespace scope can only be used in the corresponding namespace. If they are called in the global scope, they cannot be used and an undefined error will be reported.

3. Class scope

​ This scope can be seen from the name that it is the functions and data defined in the class. Of course, the data in this scope can only be used in the class. The functions in the class are called member functions, and the data are called Member data (member variables).

4. Function scope

​ Function scope refers to the data defined in the function, which also includes the formal parameters of the function. The formal parameters in this scope or the variables defined in the function can only be used in this function.

5. Fast scope: Block scope refers to the scope within a set of curly brackets. The scope of variables declared in this scope is the fast scope. The usage rate of fast scope is very small and basically not used.

4.2 Namespace

The introduction of namespace is to solve the ambiguity caused by the same functions between different libraries . Problems causing the program to fail to compile and run.

It can be used as additional information to distinguish functions, classes, variables, etc. with the same name in different libraries. A namespace defines a range.

Namespace usage:

The definition keyword of a namespace is namespace, followed by the name of the namespace.

namespace test {
	class ClassEximple;
}

The above is to define a namespace test, and declare the class ClassEximple in the test namespace. In this way, if you call a function or variable in the future, you need to add the name of the namespace in front.

Namespaces can be referenced like header files. When a namespace is introduced, it can be qualified without applying the namespace name when calling. It should be noted that if two namespaces are introduced here, the two namespaces have duplicate functions. Or data still needs to be qualified by namespace, otherwise ambiguity will still occur.

Namespace reference:

using namespace The name of the namespace, so that when using the namespace, you do not need to add the name of the namespace in front. This directive tells the compiler that subsequent code will use names in the specified namespace.

For example

using namespace std;

The above is the namespace of the introduced std standard library.

characteristic:

Namespace definitions can be superimposed, that is, when you use a namespace in a file and add class A to the namespace, you also define a namespace with the same name in another file and add class B to the namespace. in space. This namespace contains data and functions of two classes.

For example

namespace test {
	class 类A;
}
namespace test {
	class 类B;
}
这个时候命名空空间,test中就包含了类A的元素和类B的元素。

When you need to obtain the functions of classes in the namespace, you can use the "::" symbol to qualify, for example:

test::类A //这就是指定使用类A的元素,后面可以是数据类型。

Guess you like

Origin blog.csdn.net/qq_43812868/article/details/133244579