当前位置:网站首页>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::对象。
创作不易,三连支持一下吧 如有错误,敬请斧正
边栏推荐
- Fix the problem that the highlight effect of the main menu disappears when the easycvr Video Square is clicked and played
- Dab-detr: dynamic anchor boxes are better queries for Detr translation
- EasyCVR视频广场点击播放时,主菜单高亮效果消失问题的修复
- 食堂用户菜品关系系统(C语言课设)
- JS form get form & get form elements
- Case reward: Intel brings many partners to promote the innovation and development of multi domain AI industry
- On the 110th anniversary of Turing's birth, has the prediction of intelligent machine come true?
- EasyCVR集群版本添加RTSP设备提示服务器ID错误,该如何解决?
- Practice Guide for interface automation testing (middle): what are the interface testing scenarios
- [coded font series] opendyslexic font
猜你喜欢
Ssm+jsp realizes the warehouse management system, and the interface is called an elegant interface
The most complete deployment of mongodb in history
Surpassing postman, the new generation of domestic debugging tool apifox is elegant enough to use
Network Security Learning - Information Collection
Case reward: Intel brings many partners to promote the innovation and development of multi domain AI industry
SSM+jsp实现仓库管理系统,界面那叫一个优雅
AI 落地新题型 RPA + AI =?
测试/开发程序员怎么升职?从无到有,从薄变厚.......
5年自动化测试,终于进字节跳动了,年薪30w其实也并非触不可及
Video fusion cloud platform easycvr video Plaza left column list style optimization
随机推荐
GPT-3当一作自己研究自己,已投稿,在线蹲一个同行评议
Optimization of channel status offline of other server devices caused by easycvr cluster restart
一度辍学的数学差生,获得今年菲尔兹奖
用CPU方案打破内存墙?学PayPal堆傲腾扩容量,漏查欺诈交易量可降至1/30
广告归因:买量如何做价值衡量?
掌握软件安全测试方法秘笈,安全测试报告信手捏来
Lecture 3 of "prime mover x cloud native positive sounding, cost reduction and efficiency enhancement lecture" - kubernetes cluster utilization improvement practice
Deeply cultivate the developer ecosystem, accelerate the innovation and development of AI industry, and Intel brings many partners together
科兴与香港大学临床试验中心研究团队和香港港怡医院合作,在中国香港启动奥密克戎特异性灭活疫苗加强剂临床试验
Golang calculates constellations and signs based on birthdays
kivy教程之设置窗体大小和背景(教程含源码)
Web3 社区中使用的术语
Intel and Xinbu technology jointly build a machine vision development kit to jointly promote the transformation of industrial intelligence
[written to the person who first published the paper] common problems in writing comprehensive scientific and Technological Papers
[OA] excel document generator: openpyxl module
Mongo shell, the most complete mongodb in history
Comment les tests de logiciels sont - ils effectués sur le site Web? Testez la stratégie!
AI 落地新题型 RPA + AI =?
抖音或将推出独立种草社区平台:会不会成为第二个小红书
赠票速抢|行业大咖纵论软件的质量与效能 QECon大会来啦