简体   繁体   English

将一个名称空间隐藏在另一个名称空间中

[英]Hide one namespace in another

I need to use one namespace inside another and i don't want to allow access to the first namespace in the way like this 我需要在另一个内部使用一个名称空间,并且我不想以这种方式允许访问第一个名称空间

namespace One
{
    typedef int INT;
}

namespace Two
{
    using namespace One;
    #include "file.h"
    ...
}

int main(void)
{
    Two::INT i;
}

file.h 文件

namespace One 
{
    INT k;
}

I can't change file.h that is why i need using inside Two and i need user be forced to use this notation: 我无法更改file.h,这就是为什么我需要在Two内部使用,并且我需要用户被迫使用此表示法:

int main(void)
{
    Two::One::INT i;
}

Such notation is mandatory, because in file.hi redefine types from One with another pragma and if user ask for same type with Two::type_name and Two::One::type_name it would get structures with different memory layouts. 这种表示法是强制性的,因为在file.hi中,将类型从One重新定义为另一种编译指示,并且如果用户要求使用Two::type_nameTwo::One::type_name来请求相同的类型,它将获得具有不同内存布局的结构。

Yes, Two::One::k must be accessible. 是的,必须可以访问Two::One::k

// Namespaces can be nested where you can define one namespace inside another name space as follows: //可以嵌套命名空间,您可以在其中定义另一个命名空间中的一个命名空间,如下所示:

namespace namespace_name1 {
   // code declarations
   namespace namespace_name2 {
      // code declarations
   }
}

//You can access members of nested namespace by using resultion operators as follows: //您可以使用结果运算符访问嵌套名称空间的成员,如下所示:

// to access members of namespace_name2 //访问namespace_name2的成员

using namespace namespace_name1::namespace_name2;

// to access members of namespace:name1 //访问命名空间:name1的成员

using namespace namespace_name1;

// http://www.tutorialspoint.com/cplusplus/cpp_namespaces.htm // http://www.tutorialspoint.com/cplusplus/cpp_namespaces.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以在标题中“导入”一个名称空间吗? - Can I “import” one namespace into another in the header? 在模板参数的功能中使用一个名称空间或另一个 - Use one namespace or another in function of a template parameter 访问一个名称空间数据成员到另一个 - Accessing one namespace data member into another std ::需要在cpp文件中为一种类型而不是其他类型指定名称空间 - std:: namespace needs to be specified for one type but not another in the cpp file 从一个解释器中获取tcl名称空间的内容,然后传递给另一个解释器 - Grab the content of tcl namespace from one interpreter and pass to another interpreter 如何从另一个命名空间而不是全局命名空间定义函数和数据? - How to define functions and data from another namespace, not in the global one? 在一个函数中使用来自命名空间的符号,而在同一个文件中使用另一个 - using symbols from a namespace in one function but not another in the same file 在C ++中使用该名称空间将包含名称空间定义的一个文件链接到另一个文件时出现问题吗? - Problem with linking one file containing namespace definition with another using that namespace in C++? 将DirectX名称空间隐藏在我的名称空间内 - Hide DirectX namespace inside my namespace 隐藏命名空间外的类成员 - Hide class members outside namespace
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM