Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Namespace in C++

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Xiaobian to share with you the sample analysis of namespaces in C++, I hope you have gained something after reading this article, let's discuss it together!

1. namespace

In C++, names can be variables, functions, structures, enumerations, classes, and members of structures and classes. This is not a problem in itself, but as the project grows, the likelihood of conflicting names increases significantly.

For example, we use code from multiple vendors, all of which define the List, Tree, and Node classes, but they are defined differently, so there is no way to be compatible with each other. This is when we want to use one library's List class and another library's Tree class, which is very troublesome. This type of conflict is known as the namespace problem.

1.1 Traditional C++ namespaces

Let's review a few terms first.

Declaring area:

Declaring region refers to the region in which declarations can be made, for example, we can declare global variables outside the function, and for global variables, its declaration region is the file in which it is declared. For a variable declared in a function, its declaration region is the code block in which it is declared.

Potential scope:

The potential scope is more precise than the declarative area, starting at the declarative statement and continuing to the end of the declarative area. This is because variables must be defined before they can be used, so the potential scope is smaller than the declaration area.

One detail here is that variables are not necessarily visible throughout the underlying scope. Because it may also be hidden by variables of the same name nested in the declaration area. For example, if we define both a global variable and a function variable with the same name, then the outer global variable in the function will be hidden by the local variable with the same name.

Scope:

In conjunction with the foregoing, the extent to which a variable is visible to the program is called scope, which is more precise than potential scope.

1.2 New namespace attributes

C++ adds the ability to create named namespaces by defining a new declarative region, the purpose of which is to provide a declarative named region. Names in one namespace do not conflict with the same names in another namespace, while allowing other parts of the program to use what is declared in that namespace.

For example, in the C++ Primer example, two namespaces A and B are created using the new keyword namespace.

namespace A { double pail; void fetch(); int pal; struct Well {...};} namespace B { double bucket(double n) {...} double fetch; int pal; struct Hill {...};}

A namespace can be global or in another namespace, but not in a code block. Therefore, all declared names in the default namespace have external linkability, except constants modified by the const keyword.

In addition to user-defined namespaces, there is another namespace--the global namespace. It corresponds to a file-level declaration zone, so the global variables mentioned earlier are now described as being in the global namespace.

Names in any namespace do not conflict with names in other spaces, so fetches in A can coexist with fetches in B. Declarations and definition rules in namespaces bucket global declarations and definition rules are the same.

Namespaces are open, and names can be added to namespaces that have already been created, such as:

namespace A { char *goose(const char *);}

Similarly, we only defined the function fetch in namespace A before, but there is no definition. We can also add definitions in the following code:

namespace A { void fetch () { ... }}

Of course, we need a way to access names in a given namespace, and the easiest way to do that is to use the scope resolver:: to find the name using the namespace name:

A::pail = 12.34;A::fetch();

Names without scope resolvers are unqualified, and names that include namespaces are qualified.

There are many concepts involved in this article, which seems a bit obscure. But I personally feel that these concepts are not complicated to understand, mainly some explanatory language is somewhat difficult to read. The best way is to calm down, a little intensive reading, first understand the front and then look at the back.

After reading this article, I believe you have a certain understanding of "sample analysis of namespaces in C++". If you want to know more about this knowledge, please pay attention to the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report