什么是命名空间污染? [英] What is namespace pollution?

查看:638
本文介绍了什么是命名空间污染?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

命名空间污染"一词是什么意思,为什么将一种方法静态化有助于防止这种情况?

What does the term 'namespace pollution' mean and why would making a method static help to prevent it?

这个问题似乎很相似,但专门与JavaScript有关,答案并没有定义该术语.

This question seems to be similar but relates specifically to JavaScript, and the answers don't define the term.

推荐答案

名称空间只是名称存在的空间(现在看来已经足够明显了).

A namespace is simply the space in which names exist (seems obvious enough now).

比方说,您有两段代码,一个用于处理链表,另一个用于处理树.现在,这两段代码都将受益于getNext()函数,以帮助遍历数据结构.

Let's say you have two pieces of code, one to handle linked lists, the other to handle trees. Now both of these pieces of code would benefit from a getNext() function, to assist in traversal of the data structure.

但是,如果它们使用相同的名称定义该函数,则可能会发生冲突.输入以下代码后,编译器会做什么?

However, if they both define that function with the same name, you may have a clash. What will your compiler do when you enter the following code?

xyzzy = getNext (xyzzy);

换句话说,您实际上要使用哪个getNext()?有很多方法可以解决此问题,例如使用面向对象的代码:

In other words, which getNext() do you actually want to use? There are numerous ways to solve this, such as with object-oriented code, where you would use:

xyzzy = xyzzy.getNext();

,由于您已通过变量xyzzy本身指定了类型,因此这会自动神奇地选择正确的类型.

and that would auto-magically select the correct one by virtue of the fact you've specified the type via the variable xyzzy itself.

但是,即使使用的大多数是面向对象的代码,在某些情况下您也可能会发生冲突,而这正是命名空间输入图片的地方.它们使您可以将名称放在自己的区域中,以区分它们.

But, even with mostly-OO code, there may be situations where you have a conflict, and that's where namespaces enter the picture. They allow you to place the names into their own area so as to distinguish them.

C ++将其所有标准库内容放入std命名空间.如果由于某种原因需要使用与库中的函数不同的fopen()rand()函数,则可以将其放在您的 own 名称空间中以将它们分开.

C++, as one example, places all its standard library stuff into the std namespace. If, for some reason, you need an fopen() or rand() function that works differently from the one in the library, you can place it in your own namespace to keep them separate.

现在描述名称空间冲突.从技术上讲,名称空间污染只是将符号保留在实际上不应存在的名称空间中.这不一定会导致冲突,但会增加冲突的可能性.

Now that describes namespace clashes. Technically, namespace pollution is simply leaving your symbols in a namespace where they shouldn't really be. This doesn't necessarily lead to clashes but it makes it more likely.

将方法设为静态(类似C语言)的原因与名称在给定翻译单元之外(例如在链接时)可用于世界的原因有关.使用代码:

The reason why making a method static (in C-like languages) has to do with the names being made available to the world outside the given translation unit (when linking, for example). With the code:

int get42 (void) { return 42; }
int main (void) { return get42(); }

这些功能的

都可供链接器使用.

both of those functions are made available to the linker.

除非您需要从其他地方调用get42(),使其保持静态:

Unless you have a need to call get42() from somewhere else, making it static:

static int get42 (void) { return 42; }
int main (void) { return get42(); }

将防止它污染链接程序所维护的命名空间–在C语言中,将static限定符应用于文件级对象或函数将赋予其内部链接.

will prevent it from polluting the namespace maintained by the linker – in C, applying the static qualifier to a file-level object or function gives it internal linkage.

它与C ++命名空间类似,因为您可以在400个不同源文件中包含一个static int get42(),并且它们不会互相干扰.

It's similar to the C++ namespaces in that you can have a static int get42() in four hundred different source files and they won't interfere with each other.

这篇关于什么是命名空间污染?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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