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

查看:100
本文介绍了为什么以及如何在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天全站免登陆