当前位置:网站首页>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
边栏推荐
- "Digital security" alert NFT's seven Scams
- leetcode1 --两数之和
- 2022年下半年软考信息安全工程师如何备考?
- In order to improve efficiency, there are various problems when using parallelstream
- MXNet对DenseNet(稠密连接网络)的实现
- Esp32-c3 is based on blinker lighting control 10 way switch or relay group under Arduino framework
- pycharm不能输入中文解决方法
- Canal realizes MySQL data synchronization
- ES6 array de duplication new set()
- mujoco+spinningup进行强化学习训练快速入门
猜你喜欢

QGIS loading online map: Gaode, Tiandi map, etc

移动端网站,独立APP,网站排名策略有哪些?

Error: cannot find or load main class XXXX

Uncaught SyntaxError: Octal literals are not allowed in strict mode.

In order to improve efficiency, there are various problems when using parallelstream

音视频技术开发周刊 | 255

uniapp处理后台传输图片

Applet sharing function
![Serious [main] org.apache.catalina.util.lifecyclebase Handlesubclassexception initialization component](/img/39/6f6760e80acec0b02028ea2ed1a5b1.png)
Serious [main] org.apache.catalina.util.lifecyclebase Handlesubclassexception initialization component

飞盘局有多快乐?2022年轻人新潮运动报告
随机推荐
领域驱动模型设计与微服务架构落地-模型设计
Talk about your understanding of hashcode and equals methods?
window unbutu20 LTS apt,wget 安装时 DNS 解析错误
Turn off automatic update when brew executes commands
sieve of eratosthenes
Hcip day 6 notes
How to solve the problem of taking up too much space when recording and editing videos?
JS Array indexOf includes sort() 冒号排序 快速排序 去重和随机样本 random
Explain the precision of floating point numbers in detail
stable_baselines快速入门
互斥锁、自旋锁、读写锁……理清它们的区别和应用
Mutex lock, spin lock, read-write lock... Clarify their differences and applications
Programmer growth chapter 27: how to evaluate requirements priorities?
0719RHCSA
二叉树基本知识
HTTP cache tongtianpian, there may be something you want
hcip第八天实验
IM system - some common problems of message streaming
Numpy quick start
刷题-洛谷-P1059 明明的随机数