当前位置:网站首页>Namespaces and libraries
Namespaces and libraries
2022-07-25 13:44:00 【Hou Jiashu】
Why propose namespaces
stay C/C++ in , Variable 、 There are lots of functions and classes to learn later , These variables 、 The names of functions and classes will exist in the global scope , It could lead to a lot of conflict . The purpose of using a namespace is to localize the name of the identifier , To avoid naming conflicts or names Pollution ,namespace The emergence of keywords is aimed at this problem .
We hope to solve the problem of naming conflict and pollution through namespaces
Before the namespace is proposed , These names will all exist in the global scope .
The problem also arises here
Through the division of naming scope , Make the names originally named in the global scope , Separated by namespaces
All names have their own namespace
Definition of namespace
Define the namespace , Need to be used namespace keyword , Followed by the name of the namespace , And then a couple {} that will do ,{} It is named in Members of the space .
namespace name {
Statement and definition
}Three characteristics of namespaces
1. Variables can be defined in the namespace / function / type
2. Namespaces can be nested
3. Multiple namespace with the same name are allowed in the same project , The compiler will eventually synthesize in the same namespace .
Use of namespaces
Add a namespace name and scope qualifier
Through the scope qualifier , Tell the compiler which scope the name is in
int main()
{
printf("%d\n", N::a);
return 0;
}Use using Bring a member from a namespace into
using N::b;
int main()
{
printf("%d\n", N::a);
printf("%d\n", b);
return 0;
}Use using namespace Namespace name introduce
using namespce N;
int main()
{
printf("%d\n", N::a);
printf("%d\n", b);
Add(10, 20);
return 0;
}Namespace vs std( Standard namespace )
c++98 when , Namespace is introduced to solve the problem of naming conflict and pollution .
But in c++98 Before , already
There are some library functions , for example :<iostream.h>、<fstream.h> etc.
C++ The first version of the standard , Most compilers support , International Organization for Standardization (ISO) And American standardization The association recognizes , Rewrite... As a template C++ Standard library , Introduced STL( Standard template library )
Talk about all the Libraries ( Include c The library of languages ) Has been rewritten , All libraries with standard namespaces , It's not with .h Is the suffix of the file
In the rewritten Library , All names are named after std In this namespace , When calling the name in the standard library , Indicate the namespace where the name is located
边栏推荐
- Okaleido launched the fusion mining mode, which is the only way for Oka to verify the current output
- @Classmethod decorator
- 运动豪华还是安全豪华?亚洲龙与沃尔沃S60该入手哪款?
- @Wrap decorator
- JS array indexof includes sort() colon sort quick sort de duplication and random sample random
- 埃拉托斯特尼筛法
- Programmer growth chapter 27: how to evaluate requirements priorities?
- QGIS loading online map: Gaode, Tiandi map, etc
- 刷题-洛谷-P1046 陶陶摘苹果
- Prepare for 2022 csp-j1 2022 csp-s1 preliminaries video set
猜你喜欢
随机推荐
Gym安装、调用以及注册
How to refactor embedded code?
Install mujoco and report an error: distutils.errors DistutilsExecError: command ‘gcc‘ failed with exit status 1
【服务器数据恢复】HP EVA服务器存储RAID信息断电丢失的数据恢复
刷题-洛谷-P1151 子数整数
MXNet对DenseNet(稠密连接网络)的实现
Talk about your understanding of hashcode and equals methods?
Online Learning and Pricing with Reusable Resources: Linear Bandits with Sub-Exponential Rewards: Li
Sword finger offer special assault edition day 10
Basic knowledge of binary tree
Canvas判断内容为空
刷题-洛谷-P1161 开灯
The interviewer asked me: how much do you know about MySQL's storage engine?
嵌入式代码如何进行重构?
QGIS loading online map: Gaode, Tiandi map, etc
window unbutu20 LTS apt,wget 安装时 DNS 解析错误
0710RHCSA
"Digital security" alert NFT's seven Scams
Framework package merge script
Based on Baiwen imx6ull_ Ap3216 experiment driven by Pro development board








