[英]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_name
和Two::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.