namespace 的用法

 做下namespace 学习的笔记.如果有人想彻底了解namespace 可看<thinking in C++>中414页.

1.You can subdivide the global name space into more managable pieces using the namespace feature of C++.(说明了namespace 的功能).

The namespace keyword similar to class ,struct ,enum and union,puts the names of its members in a distinct space.While the other keyword  have additonal purposes. The creation of a new namespace is the only purpose for namespace.

 

namespace  Mylib

{

};

 

2. C++ deprecates the use of file statics in favor of the unnamed namespace.

namespace

{

};

int main()

{
}

 

3.you can refer to a name within a namespace in three ways by specifying the name unsing the scope resolution operator (::),

with a using directive to introduce all names in the namespace (using namespace ***;), or with a using declaration to introduce names one  at a time (using **::**);

 

4.下面这个是重点,作者实践的心得

The key thing to rember is that when you introduce a global  using directive (via a " using namespace "outside of any scope)  ,you have thrown open the namespace for that file,this is usually fine for an implementation file(a "cpp" file) .because the using directive is only in effect until the end of the compilation of that file.That is , it doesn't  affect any other files  so  you can adjust the control of the namespaces one implementation at a time.

Header files are a different issue.In header files you should either use explicit qualification or scoped using directives and using declarations.

 

总结: 在头文件定义命名空间.在头文件命名空间中,用::  或者 using **::**;

         不用using namespace ***;防止嵌套引起的冲突.

        在实现文件中 ,用using namespace ***;如果发生了命名冲突,在改用

        using **::**;

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值