c++ anonymous namespace -- 匿名空间
c++ anonymous namespace -- 匿名空间
- #include <stdio.h>
- namespace A {
- int ID = 1;
- }
- namespace {
- int ID = 11;
- }
- namespace B {
- int ID = 21;
- }
- int main(void){
- printf("ID %d \n",ID);
- }
输出
- ID 11
这里用到是C的函数,减少std空间的干扰。这里输出的是匿名空间的内容。那么就类似于如下用法
- namespace __UNIQUE_NAME_ {
- int ID;
- }
- using namespace __UNIQUE_NAME_;
如果修改代码如下
- #include <stdio.h>
- namespace A {
- int ID = 1;
- }
- namespace {
- int ID = 11;
- }
- namespace B {
- int ID = 21;
- }
- namespace {
- int ID = 41;
- }
- int main(void){
- printf("ID %d \n",ID);
- }
编译报错,如下
- t_anonymity_space.cpp:14: error: redefinition of ‘int ::ID’
- t_anonymity_space.cpp:7: error: ‘int ::ID’ previously defined here
- //file def.h
- namespace {
- int ID = 41;
- }
- #include <stdio.h>
- #include "def.h"
- namespace A {
- int ID = 1;
- }
- namespace {
- int ID = 11;
- }
- namespace B {
- int ID = 21;
- }
- int main(void){
- printf("ID %d \n",ID);
- }
- t_anonymity_space.cpp:7: error: redefinition of ‘int ::ID’
- def.h:3: error: ‘int ::ID’ previously defined here
- #include <stdio.h>
- namespace A {
- int ID = 1;
- }
- namespace {
- int ID = 11;
- int id = 12;
- }
- namespace B {
- int ID = 21;
- }
- int main(void){
- printf("ID %d \n",ID);
- using namespace A;
- printf("ID %d - %d - %d \n",A::ID, ::ID, id);
- }
- ID 11
- ID 1 - 11 - 12
c++ anonymous namespace -- 匿名空间的更多相关文章
- anonymous namespace V.S. static variant
[anonymous namespace V.S. static variant] 在C语言中,如果我们在多个tu(translation unit)中使用了同一个名字做为函数名或者全局变量名,则在链 ...
- Kubernetes K8S之Pod跨namespace名称空间访问Service服务
Kubernetes的两个Service(ServiceA.ServiceB)和对应的Pod(PodA.PodB)分别属于不同的namespace名称空间,现需要PodA和PodB跨namespace ...
- scoping作用域,anonymous function匿名函数,built-in functions内置函数
作用域练习1 def test1(): print('in the test1') def test(): print('in the test') return test1 res = test() ...
- c++ 的namespace及注意事项
前文 下文中的出现的"当前域"为"当前作用域"的简写 namepsace在c++中是用来避免不同模块下相同名字冲突的一种关键字,本文粗略的介绍了一下namesp ...
- C++ 匿名名字空间及静态非成员函数
在C++中,static有一个感觉被较少提及的用法:修饰非成员函数,这个用法实际是从C语言继承来的.其作用是表明这个函数只在当前编译单元中有效.这就使这个函数的所有引用在编译时就可以全部确定,无需进入 ...
- 【转】利用匿名namespace解决C++中重复定义的问题
目录 利用匿名namespace解决C++中重复定义的问题 原文:https://blog.csdn.net/pi9nc/article/details/11267031 利用匿名namespace解 ...
- ASP.NET MVC3控制器传递匿名对象到视图实例
ASP.NET MVC3 + Entity Framework项目中,从控制器传递匿名对象到视图非常常见,原本以为用dynamic能轻松搞定,最后发现我错了: Controller: 代码如下 复制 ...
- C++匿名命名空间
当定义一个命名空间时,可以忽略这个命名空间的名称: namespce { char c; int i; double d; } ...
- namespace、struct、enum、union、string(day01)
一 C++概述 C++历史背景 )C++的江湖地位 jave C C++ C# python )C++之父:Bjarne Stroustrup(--) ,Cpre,为C语言增加类的机制 ,Bjarne ...
随机推荐
- poi操作officePOI操作excel中的数据格式(日期类型)
7.3.3 POI中Excel文件Cell的类型 在读取每一个Cell的值的时候,通过getCellType方法获得当前Cell的类型,在Excel中Cell有6种类型,如表7-3所示. 表7-3 C ...
- Android中的单元测试
2015年5月19日 23:10 在Android中,已经内置了Junit所以不需要在导包.只要继承AndroidTestCase类就可以了. 首先需要修改AndroidManifes ...
- ArrayList集合--C#
static void Main(string[] args) { //实例化出一个集合对象 ArrayList list = new ArrayList(); /*添加*/ //--添加单个元素 l ...
- SecureCRT辅助解决方案
SecureCRT辅助解决方案 1. 下载SecureCRT 7.3版本并激活: 2. SecureCRT linux配色方案: 3. SecureCRT设置log保存方案: 1. secureCRT ...
- Python的strip()与split()
==>the start 说实话刚开始我对strip()和split()这两个还真的不太懂,后来在网上查了资料才明白. 可能别人觉着这俩很好区分,但是我最开始确实是有点分不清的,或者说不太确定这 ...
- mysql启动的四种方式
mysql的四种启动方式: .mysqld 启动mysql服务器:./mysqld --defaults-file=/etc/my.cnf --user=root 客户端连接: mysql --def ...
- Android核心基础(十一)
1.Android的状态栏通知(Notification) 通知用于在状态栏显示消息,消息到来时以图标方式表示,如下: //获取通知管理器 NotificationManager mNotificat ...
- Tuxedo入门学习
中间件介绍: 介于客户机和server之间的夹层,突破了传统的c/s架构,为构建大规模,高性能,分布式c/s应用程序提供了通信,事物,安全,容错等基础服务,屏蔽了底层应用细节,应用程序不必从底层开发, ...
- android圆形进度条ProgressBar颜色设置
花样android Progressbar http://www.eoeandroid.com/thread-1081-1-1.html http://www.cnblogs.com/xirihanl ...
- 阿根廷探戈(Argentine Tango)舞步
阿根廷探戈(Argentine Tango)舞步 阿根廷探戈(Argentine Tango)舞步 2011-11-22 13:05:11 不像其它大部分的社交舞,阿根廷探戈没有固定的舞步,它是一 ...