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

Case Analysis of Namespace in C++

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Most people do not understand the knowledge points of this "C++ Namespace case Analysis" article, so the editor summarizes the following, detailed contents and clear steps, which can be used for reference. I hope you can get something after reading this article. Let's take a look at this "C++ Namespace case Analysis" article.

Properties of namespaces

First, familiarize yourself with the two concepts of namespaces.

Declaration region: the area in which you can declare, such as the declaration area of a global file is a file, and the variable declaration area declared within a function is a code block. Potential scope: the potential scope starts at the declaration point and ends at the end of its declaration area. Therefore, the potential scope is smaller than the declaration area, because it must be defined before it can be used.

These two concepts are easy to understand through the figure below, and are also common sense of programming.

Declare namespaces and namespace members

You can declare namespaces and space members using the following syntax

Namespace Jack {double a; struct b {}; void f ();} global namespace

If an identifier is not declared in an explicit namespace, the identifier is part of the implicit global namespace. In general, try to avoid declaring globally (except for the entry point Main function, which must be in the global namespace).

The global namespace can be called with:: var.

Discontiguous namespace

That is, the same namespace can be defined in multiple files, such as

File1.cpp

Namesapce A {int a;}

File2.cpp

Namesapce A {int b;}

The same namespace scattered in each file is grouped together at compile time.

Nested namespace

Namespaces can also be nested

Namespace A {namespace B {}} using declaration and using compilation directive

The using declaration adds a specific name to the declaration area to which it belongs, such as using using namespace std;, which is equivalent to a static import in Java, while the using compilation instruction makes all names of the namespace available without using parsers, such as using namespace std;,. Import java.util.* . Make a note here and pay attention.

Using A-V-A-V-V-P-L-L-H-L-L-H-L-H-P-L-H-L-H-P-H-P-L-H-L-L-H-L-N-O-N-D-N-L-L-N-L-N-N-L-N-L-N-H-H-N-H-H-L-N-H-H-P-L-H-L-H-P-L-R-L-H-H-L-H-H-H

The above code can cause conflicts.

It is generally safer to use using declarations.

Namespace alias

Namespace names must be unique, which means that usually they should not be too short. If the length of the name makes the code difficult to read, or if you type boring in a header file that cannot use the using directive, you can create a namespace alias as an abbreviation for the actual name. For example:

Namespace a_very_long_namespace_name {class Foo {};} namespace AVLNN = a roomverylongitude namespacewornname void Bar (AVLNN::Foo foo) {} synthesis code example # include using namespace std;namespace A {int a = 100; namespace B / / nesting a namespace B {int a = 20;}} int a = 200 / / define a global variable int main (int argc, char * argv []) {cout

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