简体   繁体   English

从另一个名称空间访问标准名称空间数据成员

[英]Accessing standard namespaces data members from another namespace

If the code is written as below: 如果代码编写如下:

#include<iostream>

namespace n2    {
    int y = 10;
}

namespace n1    {
    int x =  20;
    int m = ::n2::y;

    std::string str;
}

int main()
{
    std::cout << n1::x << std::endl;
    std::cout << n2::y << std::endl;

    return 0;
}

Then my question is, if i have not make use of the statement, 然后我的问题是,如果我没有使用该语句,

using namespace std 使用命名空间标准

Then I have to manually mention the data-member along with namespace as std::string (for example), but if I make use of the same data member (std::string) inside the another namespace, eg namespace n1, then will it not be implicitly changed to the namespace n1::std::string? 然后,我必须手动将数据成员以及名称空间作为std :: string提及(例如),但是如果我在另一个名称空间(例如名称空间n1)中使用相同的数据成员(std :: string),则将它不会隐式更改为名称空间n1 :: std :: string吗?

  1. There is no n1::std::string data type, then how it is working. 没有n1 :: std :: string数据类型,那么它是如何工作的。 How n1::std::string is getting converted to ::std::string? n1 :: std :: string如何转换为:: std :: string?

  2. But why it remains aloof from relative usage inside another namespace. 但是为什么它仍然与另一个名称空间内的相对用法保持距离。 If I wanted to make use of that concept in my code, how do I change the code. 如果我想在代码中使用该概念,该如何更改代码。 If the same concept to be used for my own library, how do i make the changes, to make use of the same concept. 如果将相同的概念用于我自己的库,那么我该如何进行更改以使用相同的概念。

In i1::i2 , i1 is an unqualified identifier and is looked up as any other unqualified identifier. i1::i2 ,i1是一个不合格的标识符,并且像其他任何不合格的标识符一样被查找。 First in the current scope, then in the enclosing one until the global namespace is searched. 首先在当前作用域中,然后在封闭的作用域中,直到搜索全局名称空间。 Thus with std::string as there is no std in n1 , the std under the global namespace is found. 因此,使用std::string因为n1没有std ,因此找到了全局命名空间下的std In other words, in n1 , you can reference ::n2::y as n2::y as long as you don't have an entity named n2 in n1 . 换句话说,在n1 ,你可以参考::n2::yn2::y只要你没有一个实体的命名也n2n1

If you want to protect against a n1::std::string entity, you'll have to use ::std::string instead of std::string . 如果要防止使用n1::std::string实体,则必须使用::std::string而不是std::string

From within a namespace you can see everything in that namespace, and everything in the enclosing namespace, all the way out to the global namespace. 从一个名称空间中,您可以看到该名称空间中的所有内容,以及包含在该名称空间中的所有内容,一直到全局名称空间。 In this case namespace context has a wider meaning than the namespace keyword. 在这种情况下,名称空间上下文比名称空间关键字具有更广泛的含义。 It includes any context, such as classes, and functions. 它包括任何上下文,例如类和函数。

From within n1, you can see std because you can see everything within the global namespace, so it is implicitly ::std 在n1内,您可以看到std,因为您可以看到全局名称空间中的所有内容,因此它是隐式:: std

If, within n1 you were to define a namespace std {} then you would entirely obscure that global namespace std from your current context. 如果在n1内定义一个命名空间std {},那么您将完全不了解当前上下文中的全局命名空间std。 You could still access it either by doing a 'using' rename or by explicit ::std 您仍然可以通过“使用”重命名或通过显式:: std来访问它

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM