我想知道我们创建的名称空间存储在哪里? [英] i want to know that where are the namespaces created by us are stored ?

查看:250
本文介绍了我想知道我们创建的名称空间存储在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我们创建的名称空间存储在哪里?如果我想创建自己的命名空间,则将其保存在哪里? std命名空间存储在哪里?

i want to know that where are the namespaces created by us are stored ? if i want to make my own namespace then where to save it? where is the std namespace is stored ?

推荐答案

您不将其存储在特定的位置,只需声明它,然后将类/函数放入其中即可,例如这个:
You don''t store it anywhere specific, you just declare it, and put your classes/functions in it, like this:
namespace YourNameSpace
{
   class SomeClass { };

   void SomeFunction() { }
}


请注意,名称空间是包装概念,而不是类型.也就是说,不能创建名称空间的实例.命名空间是用于打包库的复杂机制.如果可以访问图书馆的所有内部物品,则包装只不过是一袋物品.通常,图书馆打算向外界展示一些物品,其他所有物品都供其内部使用.与公开整个名称空间相比,要导出的少量项目引起的名称冲突少得多. C ++库头中除operator delete和operator new以外的所有其他名称都在std名称空间中或在std名称空间内嵌套的名称空间中定义.包含C ++库头不会将任何库名称引入当前名称空间.例如,您将名称cin称为std :: cin.或者,您可以编写声明:
Note that namespace is a packaging concept, not a type. That is, one cannot create instances of a namespace. A namespace is a sophisticated mechanism for packaging libraries. If all the internals of a library are accessible, the packaging is no more than a bag of items. Generally, there are a few items that a library intends to present to the outside world, and everything else is for its own internal use. The few items to be exported cause much less name clash than exposing the entire namespace. All names other than operator delete and operator new in the C++ library headers are defined in the std namespace, or in a namespace nested within the std namespace. Including a C++ library header does not introduce any library names into the current namespace. You refer to the name cin, for example, as std::cin. Alternatively, you can write the declaration:
using namespace std;


(使用using声明使我们能够从没有前缀namespace_name ::前缀的命名空间访问名称.)

包括标准标头< cstdlib>有效地包括标准标头< stdlib.h>在std名称空间中.


(A using declaration allows us to access a name from a namespace without the prefix namespace_name:: )

Include the standard header <cstdlib> to effectively include the standard header <stdlib.h> within the std namespace.

#include <stdlib.h>

namespace std {
using ::size_t; using ::div_t; using ::ldiv_t;
using ::abort; using ::abs; using ::atexit;
using ::atof; using ::atoi; using ::atol;
using ::bsearch; using ::calloc; using ::div;
using ::exit; using ::free; using ::getenv;
using ::labs; using ::ldiv; using ::malloc;
using ::mblen; using ::mbstowcs; using ::mbtowc;
using ::qsort; using ::rand; using ::realloc;
using ::srand; using ::strtod; using ::strtol;
using ::strtoul; using ::system;
using ::wcstombs; using ::wctomb;
#if __IBMCPP_TR1__
namespace tr1 {
using ::lldiv_t; using ::llabs; using ::lldiv;
using ::atoll; using ::strtoll; using ::strtoull;
using ::strtof; using ::strtold;
}
#endif
}


在一种情况下,我们应该始终使用完全限定的库名:头文件内.原因是预处理器将标头的内容复制到我们的程序文本中. #include文件时,标题的确切文本就好像是文件的一部分.
如果我们在标题中放置一个using声明,则等同于在每个包含标题的程序中都放置相同的using声明,无论该程序是否需要using声明.绝对必要.


There is one case in which we should always use the fully qualified library names: inside header files. The reason is that the contents of a header are copied into our program text by the preprocessor. When we #include a file, it is as if the exact text of the header is part of our file.
If we place a using declaration within a header, it is equivalent to placing the same using declaration in every program that includes the header whether that program wants the using declaration or not .In general, it is good practice for headers to define only what is strictly necessary.


您可能想问:命名空间如何存储在编译器和链接器生成的输出文件中?这实际上是一个有趣的问题.

命名空间通常用作允许具有相同名称的多个函数驻留在同一程序中而不会引起麻烦的工具.因此,所有这些功能必须由链接器以某种独特的方式进行识别.这就是名称空间的全部意义.

因此,您的问题的答案是:编译器通过在名称前或后加后缀将该名称空间的名称添加到该函数的名称中.顺便说一句,区分具有不同参数的函数的多个版本(也称为重载)的作用相同.

这是一个例子.在我的源代码中,我有:
You probably meant to ask: How are namespaces stored in the output files the compiler and linker generate? And that is in fact an interesting question.

Namespaces often serve as a tool to allow multiple functions with the same name to reside inside the same program, without getting into way. And hence all those functions must be identified somehow by the linker in a unique way. That''s what namespaces are all about.

So, the answer to your question is: The compiler adds the name of the namespace to the function''s name by pre- or postfixing that name. By the way, it does the same thing to distinguish multiple versions of a function that differ in their arguments -- also called overloading.

Here is an example. In my source code I had:
namespace MyNamespace
{
    int MyFunction (const char* pIn)
    {
        return 5;
    }
}



现在,我设置了项目设置,以便链接器生成链接图.在该图中,我在名称下找到了上面的函数:



Now I set the project settings so that the linker produces a link map. In that map I find the above function under the name:

?MyFunction@MyNamespace@@YAHPBD@Z



这就是另一个程序单元如何通过重构那个被人为操纵的"名称在命名空间MyNamespace中找到MyFunction的方式. YAHPBD @ Z是编译器用来对函数的参数的数量和类型进行编码的组合.

请注意,在函数名前后加上前缀的方法取决于编译器的实现.因此,一生中都不必依赖它.编译器供应商只保证语言定义中的内容,即您的命名空间完全分开.



and that''s how another program unit can find MyFunction in namespace MyNamespace, by reconstructing that very "mangeled" name. The YAHPBD@Z is the mumbo jumbo the compiler uses to encode the number and type of arguments of the function.

Note that the methode of pre- or postfixing the function name is dependent on the compiler implementation. So it is nothing to rely on for a life time. The compiler vendor just guarantees what is in the language definition, namely that your namespaces are nicely separated.


这篇关于我想知道我们创建的名称空间存储在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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