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

What are the C++ namespaces and their features

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article shows you the C++ namespace and what its features are, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.

I. the traditional C++ namespace

Declaration area (declaration region): a declaration area is an area in which you can declare it. For example, you can declare a global variable outside the function, and for such a variable, its declaration area is the file in which it is declared. For a variable declared in a function, the declaration area is the block of code in which it is declared.

Potential scope (potential scope): the potential scope of a variable 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 the variable cannot be used until it is defined.

II. New namespace features

C++ includes the ability to create named namespaces by defining a new declaration area, one of the purposes of which is to provide a region for declaring names. A name in one namespace does not conflict with the same name in another namespace, while allowing other parts of the program to use what is declared in that namespace. For example, use the keyword namespace to create two namespaces:

Namespace Jack {double pail; / / variable declaration void fetch (); / / function prototype int pal; / / variable declaration struct Well {...} / / structure declaration} namespace Jill {double bucket (double n) {...}; / / variable declaration double fetch / / variable declaration int pal; / / variable declaration struct Hill {...} / / structure declaration}

Namespaces are open, that is, names can be added to existing namespaces. For example, the following statement adds the name goose to the list of names already in Jill:

Namespace Jill {char* goose (const char*);}

Again, the original Jack namespace provides a prototype for the fech () function. You can use the Jack namespace again after this file (or in another file) to provide the code for the function:

Namespace Jack {void fetch () {...}}

When you need to access the name of a given namespace, use the namespace to qualify the name through the scope parsing operator::.

1.using declaration and using compilation directive

When we don't want to qualify a name every time we use it, C++ provides two mechanisms (using declaration and using compilation directive) to simplify the use of names in the namespace.

Using declaration: make the identifier of a specific declaration available

Namespace Jill {double bucket (double n) {...} double fetch;} char fetch;int main () {using Jill::fetch / / using declaration double fetch; / / Error! Already have a local fetch cin > > fetch; / / read a value into Jill::fetch cin > >:: fetch; / / read a value into global fetch}

In this code, the using declaration adds a specific name to the declaration area to which it belongs. The using declaration in main () adds fetch to the declaration area defined by main (). Once the declaration is complete, you can use the name fetch instead of Jill::fetch.

Using compilation directive: make the entire namespace available

The using compilation directive makes all names available. Using the using compilation directive in the global declaration area makes the name of the namespace available globally. For example:

# include using namespace std; / / using the using compilation instruction in the function will make the name in it available in the function int main () {using namespace jack; / / make names available in vorn ()}

Different namespaces represent different memory units. There are ambiguity problems in the following situations, which should be paid attention to when using them.

Using namespace jackusing namespace jill / / there is a pal variable pal = 4; / / which one in both spaces. Now have a conflict

In general, using the using declaration is safer than using the using compilation instruction because it imports only the specified name. If the name conflicts with the local name, the compiler issues an instruction. The using compilation directive imports all names, including some that are not actually needed. If there is a conflict with a local name, the version of the namespace is partially overwritten and the compiler does not issue a warning.

two。 Other properties of namespaces

Namespaces can be nested

Namespace elements {namespace fire {int flame;...} float water;}

Access to flame refers to elements::fire::flame, and you can also use the using compilation instruction to make the internal name available: using elements::fire

Use the using compilation directive and using declaration in the namespace, as follows:

Namespace myth {using Jill::fetch; using namespace elements; using std::count;}

If you want to access Jill::fetch, you can access it in both ways.

Myth::fetchJill::fetch

Transitivity of namespaces

Using compilation instructions can be passed. If An op B and B op C, then An op C.

The above and the following two sentences of using namespace myth;/// are equivalent to using namespace myth;using namespace elements;// to create an alias for the namespace namespace MEF = myth::elements::fire;using MEF::flame

Unnamed namespace

Is often a substitute for static variables.

Static int counts / / Global declaration static storage, internal linkage/// is equivalent to namespace {int counts / / static storage, internal linkage} the above content is the C++ namespace and what its features are, have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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