当前位置:网站首页>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::对象。
创作不易,三连支持一下吧 如有错误,敬请斧正
边栏推荐
- True Global Ventures新成立的1.46亿美元后续基金关账,其中普通合伙人认缴6,200万美元以对后期阶段的Web3赢家进行投资
- 每人每年最高500万经费!选人不选项目,专注基础科研,科学家主导腾讯出资的「新基石」启动申报
- 一度辍学的数学差生,获得今年菲尔兹奖
- [record of question brushing] 2 Add two numbers
- Nanopineo use development process record
- How do test / development programmers get promoted? From nothing, from thin to thick
- [multi threading exercise] write a multi threading example of the producer consumer model.
- VIM - own active button indent this command "suggestions collection"
- Ssm+jsp realizes the warehouse management system, and the interface is called an elegant interface
- Unity3d can change colors and display samples in a building GL material
猜你喜欢
超越Postman,新一代国产调试工具Apifox,用起来够优雅
This "advanced" technology design 15 years ago makes CPU shine in AI reasoning
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
buildroot的根文件系统提示“depmod:applt not found”
How to open win11 remote desktop connection? Five methods of win11 Remote Desktop Connection
Continuous learning of Robotics (Automation) - 2022-
Win11控制面板快捷键 Win11打开控制面板的多种方法
MySQL forgot how to change the password
Break the memory wall with CPU scheme? Learn from PayPal to expand the capacity of aoteng, and the volume of missed fraud transactions can be reduced to 1/30
EasyCVR平台接入RTMP协议,接口调用提示获取录像错误该如何解决?
随机推荐
Nanopineo use development process record
食堂用户菜品关系系统(C语言课设)
leetcode 53. Maximum subarray maximum subarray sum (medium)
SSM+jsp实现仓库管理系统,界面那叫一个优雅
Unity3d can change colors and display samples in a building GL material
机器人(自动化)课程的持续学习-2022-
Digital chemical plant management system based on Virtual Simulation Technology
两个div在同一行,两个div不换行「建议收藏」
一度辍学的数学差生,获得今年菲尔兹奖
Deeply cultivate the developer ecosystem, accelerate the innovation and development of AI industry, and Intel brings many partners together
The most complete security certification of mongodb in history
Win11控制面板快捷键 Win11打开控制面板的多种方法
AI 落地新题型 RPA + AI =?
一图看懂!为什么学校教了你Coding但还是不会的原因...
Opencv third party Library
See Gardenia minor
Different meat customers joined hands with Dexter to launch different hamburgers in some stores across the country
Practice Guide for interface automation testing (middle): what are the interface testing scenarios
Data security -- 12 -- Analysis of privacy protection
Mathematical analysis_ Notes_ Chapter 10: integral with parameters