为什么以及如何在 C++ 中使用命名空间? [英] Why and how should I use namespaces in C++?

查看:19
本文介绍了为什么以及如何在 C++ 中使用命名空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前从未在我的代码中使用过命名空间.(除了使用 STL 函数)

I have never used namespaces for my code before. (Other than for using STL functions)

  1. 除了避免名称冲突,还有其他理由使用命名空间吗?
  2. 我必须将声明和定义都包含在命名空间范围内吗?

推荐答案

这里是一个很好的理由(除了你所说的显而易见的).

Here is a good reason (apart from the obvious stated by you).

由于命名空间可以是不连续的并且分布在翻译单元中,它们也可以用于将接口与实现细节分开.

Since namespace can be discontiguous and spread across translation units, they can also be used to separate interface from implementation details.

命名空间中的名称定义可以在同一个命名空间中提供,也可以在任何封闭的命名空间中提供(具有完全限定的名称).

Definitions of names in a namespace can be provided either in the same namespace or in any of the enclosing namespaces (with fully qualified names).

这篇关于为什么以及如何在 C++ 中使用命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆