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 ...
随机推荐
- 亚马逊带Marketplace product code的image无法再mount到其他镜像上
这是对已发布镜像的保护么?难道对其进行修改的路彻底断掉了? 以volume形式attach也不行,dd成raw再读取也读不了,敢问路在何方呢 If a volume has an AWS Market ...
- c#取出LDAP SearchResult所有属性
string aaa = System.Threading.Thread.CurrentPrincipal.Identity.Name; DirectorySearcher ds = new Dire ...
- Linux命令: chown
touch auth.log root@ubuntu:/work# ls -l auth.log -rw-r--r-- 1 root root 0 Feb 18 19:27 auth.log chow ...
- 我的Python成长之路---第二天---Python基础(7)---2016年1月9日(晴)
再说字符串 一.字符串的编码 字符串的编码是个很令人头疼的问题,由于计算机是美国人发明的,他们很理所当然的认为计算机只要能处理127个字母和一些符号就够用了,所以规定了一个字符占用8个比特(bit)也 ...
- HDOJ 4007 Dave【最大覆盖集】
Dave Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Submis ...
- 高级UIKit-09(TCPSocket发送文件、上传和下载)
[day1101_SocketSendFile]:发送文件到服务端 发送文件需要在该文件上拼接消息头,比如类型,文件名,文件大小 // 服务端 - (void)viewDidLoad { [super ...
- NET Core 构成体系
NET Core 构成体系 简析 .NET Core 构成体系 Roslyn 编译器 RyuJIT 编译器 CoreCLR & CoreRT CoreFX(.NET Core Librarie ...
- Jquery 中each循环嵌套的使用示例教程
1.从MVC返回的Json数据如下: 2.下面是客户端实现的示例: $.post("/admin/GetPermissionsForRole", function (data,st ...
- 【C/C++多线程编程之四】终止pthread线程
多线程编程之终止pthread线程 Pthread是 POSIX threads 的简称,是POSIX的线程标准. 终止线程似乎是多线程编程的最后一步,但绝不是本系列教 ...
- POJ 2031 prim
Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 4400 Accepted: 2 ...