如何正确使用名称空间以避免名称冲突? [英] How to properly use namespaces to avoid name collision?

查看:110
本文介绍了如何正确使用名称空间以避免名称冲突?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于C ++名称空间的正确用法,我有些困惑.对我来说,很清楚它们如何帮助避免冲突(名称冲突),但是对于using关键字,现在还不清楚.我的意思是,假设我有一部分代码放入了名称空间,并创建了一个类,例如

I'm a bit confused concerning proper usage of C++ namespaces. It is clear for me how they can help to avoid conflicts (name collision), but it is not clear anymore when it comes to the using keyword. I mean, suppose I have a part of my code that I put into a namespace, and create a class, say

namespace my
{
    class vector { ... };
}

当然,当我使用它时,我不想一直输入my::vector,所以我想使用using namespace my.但是,最终我可能需要std名称空间中的某些内容,然后同时需要using namespace std,但这将使我回到最初的名称冲突问题.

Of course, when I use it, I wouldn't like to type my::vector all the time, so I'd like using namespace my. However, I could eventually need something from the std namespace, and then I want using namespace std at the same time, but this will bring me back to the initial name collision problem.

我知道可以仅导入"我需要的功能,例如using std::set,但是在这种情况下,完全像我一样完全导入标准命名空间stdmy命名空间是很自然的. d一直都使用它们.

I know that it is possible to "import" only the functionality that I need, like using std::set, but in this case it seems natural to import both the standard namespace std and my namespace completely as I'd use both of them all the time.

这是否意味着即使我使用名称空间,我仍然应该考虑为我的类型赋予非通用名称吗?还是using namespace是错误的,我应该始终键入my::vector吗?谢谢.

Does this mean that even when I use namespaces I should still think about giving non-common names to my types? Or is using namespace a mistake and I should always type my::vector instead? Thanks.

好吧,我可能应该澄清一下,它比输入更像一个可读性问题.到处都有很多::对我来说真的很奇怪.我知道这是关于品味和习惯的问题,但是.

Well, I should probably clarify that it is more a question of readability than typing. Lots of :: everywhere look really weird to me. I know it's a question of taste and habits, but nevertheless.

推荐答案

当然,当我使用它时,我不想一直输入my :: vector,所以我想使用命名空间my.但是,最终我可能需要std命名空间中的某些内容,然后我希望同时使用命名空间std,但这会使我回到最初的名称冲突问题.

Of course, when I use it, I wouldn't like to type my::vector all the time, so I'd like using namespace my. However, I could eventually need something from the std namespace, and then I want using namespace std at the same time, but this will bring me back to the initial name collision problem.

是的,它将带您回到最初的名称冲突问题.这就是为什么您应该谨慎使用using namespace ...;指令,并且仅在源文件中使用,而不在标头中使用.

Yes, it would bring you back to the initial name collision problem. This is why you should use using namespace ...; directives sparingly, and only in source files, never in headers.

这是否意味着即使我使用名称空间,我仍然应该考虑为我的类型提供非通用名称吗?

Does this mean that even when I use namespaces I should still think about giving non-common names to my types?

不,你不应该.正是为了避免这种情况而发明了命名空间.

No, you shouldn't. Namespaces were invented precisely to avoid this.

还是使用命名空间是一个错误,我应该总是输入my :: vector来代替吗?

Or is using namespace a mistake and I should always type my::vector instead?

如果需要,可以使用using namespace ...;using ...;指令,直到出现冲突.这意味着,当您要做发生冲突时,您将通过在某些地方显式地将名称加以量化来写出不自然的"代码.

You can, if you want to, use the using namespace ...; or using ...; directives until you get conflicts. This means that when you do have conflicts, you'll end up writing "unnatural" code by explicitly quallifying names in some places.

在实践中,当您处理短名称空间名称(即std)时,您可以一直无时无刻地键入它们.大约一周后,您甚至不会注意到您正在输入它.

In practice, when you're dealing with short namespace names (i.e. std), you can just type them explicitly all the time. After a week or so, you won't even notice you're typing it anymore.

这篇关于如何正确使用名称空间以避免名称冲突?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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