博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
using
阅读量:4959 次
发布时间:2019-06-12

本文共 1910 字,大约阅读时间需要 6 分钟。

The keyword using is used to introduce a name from a namespace into the current declarative region. For example:

// using#include 
using namespace std;namespace first{ int x = 5; int y = 10;}namespace second{ double x = 3.1416; double y = 2.7183;}int main () { using first::x; using second::y; cout << x << endl; cout << y << endl; cout << first::y << endl; cout << second::x << endl; return 0;}

 

52.7183103.1416

Notice how in this code, x (without any name qualifier) refers to first::x whereas y refers to second::y, exactly as our using declarations have specified. We still have access to first::y and second::x using their fully qualified names.

The keyword using can also be used as a directive to introduce an entire namespace:

// using#include 
using namespace std;namespace first{ int x = 5; int y = 10;}namespace second{ double x = 3.1416; double y = 2.7183;}int main () { using namespace first; cout << x << endl; cout << y << endl; cout << second::x << endl; cout << second::y << endl; return 0;}

 

5103.14162.7183

 In this case, since we have declared that we were using namespace first, all direct uses of x and y without name qualifiers were referring to their declarations in namespace first.

using and using namespace have validity only in the same block in which they are stated or in the entire code if they are used directly in the global scope. For example, if we had the intention to first use the objects of one namespace and then those of another one, we could do something like:

// using namespace example#include 
using namespace std;namespace first{ int x = 5;}namespace second{ double x = 3.1416;}int main () { { using namespace first; cout << x << endl; } { using namespace second; cout << x << endl; } return 0;}

 

53.1416

 

http://www.cplusplus.com/

 

转载于:https://www.cnblogs.com/centerall/p/3434332.html

你可能感兴趣的文章
SQL中Group By的使用
查看>>
错误org/aopalliance/intercept/MethodInterceptor解决方法
查看>>
Pylint在项目中的使用
查看>>
使用nginx做反向代理和负载均衡效果图
查看>>
access remote libvirtd
查看>>
(4) Orchard 开发之 Page 的信息存在哪?
查看>>
ASP.NET中 GridView(网格视图)的使用前台绑定
查看>>
深入了解Oracle ASM(二):ASM File number 1 文件目录
查看>>
Boosting(提升方法)之AdaBoost
查看>>
链接元素<a>
查看>>
Binding object to winForm controller through VS2010 Designer(通过VS2010设计器将对象绑定到winForm控件上)...
查看>>
Spring Boot实战笔记(二)-- Spring常用配置(Scope、Spring EL和资源调用)
查看>>
活现被翻转生命
查看>>
POJ 1228
查看>>
SwaggerUI+SpringMVC——构建RestFul API的可视化界面
查看>>
springmvc怎么在启动时自己执行一个线程
查看>>
流操作的规律
查看>>
Python基础学习15--异常的分类与处理
查看>>
javascript运算符的优先级
查看>>
React + Redux 入门(一):抛开 React 学 Redux
查看>>