In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces what is the use of the namespace namespace in CCompact +. It is very detailed and has a certain reference value. Friends who are interested must read it!
Overview
Namespaces (namespace) can help us distinguish functions, classes, variables with the same name in different libraries. The context is defined by using a namespace. A namespace defines a scope.
Namespace
In order to solve the naming conflicts between identifiers in C++ standard library and global identifiers in programs, as well as between all identifiers in different libraries. All identifiers of the standard C++ library are defined in a namespace named std. When the C++ standard library is used in the program, std is used as the limit.
We used the namespace (namespace) when we wrote "Hello World":
The role of namespaces
Namespaces are scopes introduced by ANSI C++ that can be named by users to deal with common conflicts of the same name in programs. In C++ programs, there are a large number of variables, functions and class names. If they all exist in the same global namespace, it will cause a lot of conflicts.
Custom function has the same name as standard library function.
The name used in the program is the same as the name in the third-party library
Name conflicts in different modules of the same project
Namespace mechanism that localizes the names declared in it:
The same name can be used in different contexts without causing name conflicts
The standard libraries in C++ are defined in the std namespace, reducing conflicts with user-defined names
Create your own namespace in your own program to avoid conflicts between localized names and the outside world
Custom namespace
We can define our own namespaces. We separate some global entities from other global entities by placing them in separate namespaces.
Format:
Namespace Namespace name {declare;}
Variables (can be initialized) and constants
Function (which can be defined or declared)
Structural body
Class
Template
Namespace (define another namespace in one namespace, that is, a nested namespace)
The role of namespaces is to establish separate scopes. Separate some global entities to avoid name conflicts.
Methods for namespace members
General method: when referencing a namespace member, qualify the namespace member with a namespace name to distinguish the identifier of the same name. (namespace:: namespace member name)
A mechanism to simplify the use of namespace members:
Use the namespace alias: namespace TV = Television; / / TV is equivalent to Television
Use format: using namespace member name using ns1::Student; / / Student is equivalent to ns1::Student
Use: using namespace Namespace name
Case
H2.h:
# ifndef PROJECT8_H1_H#define PROJECT8_H1_Hnamespace kitchen {class Table {private: int legs; int price; public: Table (int l, int p) {legs = l; price = p;}};} # endif / / PROJECT8_H1_H
H3.h:
# ifndef PROJECT8_H2_H#define PROJECT8_H2_Hnamespace office {class Table {private: int rows; int cols; public: Table (int r, int c) {rows = r; cols = c;}};} # endif / / PROJECT8_H2_H
Mian.cpp:
# include # include "h2.h" # include "h3.h" using namespace std;int main () {kitchen::Table T1 (6,300); office::Table T2 (6,2); return 0;} above is all the content of the article "what's the use of namespace namespace in Cannabash +". Thank you for reading! Hope to share the content to help you, more related knowledge, 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.