当前位置:网站首页>namespace基础介绍
namespace基础介绍
2022-07-06 22:04:00 【就要 宅在家】
在与他人的合作编程中,可能会发生命名冲突的情况。而且项目越大,命名冲突所引发的麻烦就越大。
那么有没有什么办法能让我们即便使用相同的名称也不会发生使用冲突呢,这里就要介绍C++语法里的namespace命名空间了。
目录
一。基础用法
namespace命名空间可以存储变量,结构体,函数等等。在使用时我们需要在定义的namespace中进行声明。
例如我们想要定义两个函数car,又需要在同一个主函数中进行调用,那就可以定义两个namespace来分别存储这两个函数。在主函数中直接调用namespace即可
调用namespace我们需要使用作用域符号(::)。例如在下方程序中,当我们想使用BYD的car时,只需要在主函数中调用BYD::car()即可。这相当于在告诉计算机,当它看到::符号了,就需要在BYD这个namespace中找到car函数。别的地方你就别找了,找到也不是。
函数的定义可以直接在命名空间namespace中直接进行,当然,也可以像下方程序一样在域外定义。这时,就需要在定义函数时,先调用命名空间,让计算机知道我这是要定义某个namespace里的函数。

二。标准库
在使用标准库std的时候,我们有三种调用方法。
一,是在每次调用标准库函数时使用std::。

二,是将标准库定义为全局使用,即在头部声明using namespace std; 。

第一种方法使用太过繁琐,第二种定义为全局又可能引起冲突,这时我们就可以使用第三种定义方法。
三,将高频对象定义为全局使用。即using std::对象。

创作不易,三连支持一下吧 如有错误,敬请斧正
边栏推荐
- Hardware development notes (10): basic process of hardware development, making a USB to RS232 module (9): create ch340g/max232 package library sop-16 and associate principle primitive devices
- EasyCVR无法使用WebRTC进行播放,该如何解决?
- 见到小叶栀子
- 赠票速抢|行业大咖纵论软件的质量与效能 QECon大会来啦
- 日常工作中程序员最讨厌哪些工作事项?
- Analysis on the thinking of college mathematical modeling competition and curriculum education of the 2022a question of the China Youth Cup
- 主设备号和次设备号均为0
- EasyUI export excel cannot download the method that the box pops up
- Win11图片打不开怎么办?Win11无法打开图片的修复方法
- 機器人(自動化)課程的持續學習-2022-
猜你喜欢

英特尔与信步科技共同打造机器视觉开发套件,协力推动工业智能化转型

Win11远程桌面连接怎么打开?Win11远程桌面连接的五种方法
![[multi threading exercise] write a multi threading example of the producer consumer model.](/img/8a/4a2836f905968f42e0ef339d900b19.jpg)
[multi threading exercise] write a multi threading example of the producer consumer model.

What if the win11 screenshot key cannot be used? Solution to the failure of win11 screenshot key
![[system management] clear the icon cache of deleted programs in the taskbar](/img/cc/7aff85f1a33ef390623652eadb6f03.png)
[system management] clear the icon cache of deleted programs in the taskbar

What if win11 pictures cannot be opened? Repair method of win11 unable to open pictures

【实践出真理】import和require的引入方式真的和网上说的一样吗

EasyCVR无法使用WebRTC进行播放,该如何解决?

Why does WordPress open so slowly?

The easycvr platform is connected to the RTMP protocol, and the interface call prompts how to solve the error of obtaining video recording?
随机推荐
Hangzhou Electric 3711 binary number
Highly paid programmers & interview questions. Are you familiar with the redis cluster principle of series 120? How to ensure the high availability of redis (Part 1)?
机器人(自动化)课程的持续学习-2022-
深耕开发者生态,加速AI产业创新发展 英特尔携众多合作伙伴共聚
[OA] excel document generator: openpyxl module
Dab-detr: dynamic anchor boxes are better queries for Detr translation
Practice Guide for interface automation testing (middle): what are the interface testing scenarios
Win11图片打不开怎么办?Win11无法打开图片的修复方法
B站大佬用我的世界搞出卷积神经网络,LeCun转发!爆肝6个月,播放破百万
接口自动化测试实践指导(中):接口测试场景有哪些
【实践出真理】import和require的引入方式真的和网上说的一样吗
MySQL forgot how to change the password
MySQL split method usage
What if win11 pictures cannot be opened? Repair method of win11 unable to open pictures
Mathematical analysis_ Notes_ Chapter 10: integral with parameters
AI表现越差,获得奖金越高?纽约大学博士拿出百万重金,悬赏让大模型表现差劲的任务
[coded font series] opendyslexic font
数学分析_笔记_第10章:含参变量积分
Digital chemical plant management system based on Virtual Simulation Technology
[team learning] [34 issues] scratch (Level 2)