C++ 什么是qualified-id/name 和unqualified-id/name?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7257563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are qualified-id/name and unqualified-id/name?
提问by M3taSpl0it
I was wondering if someone could explain there terms since I encounter them in many places. I know some basic theory about them but not sure what I know is right or wrong.
我想知道是否有人可以解释这些术语,因为我在很多地方都遇到过它们。我知道一些关于它们的基本理论,但不确定我所知道的是对还是错。
So can any one please explain these terms?
那么任何人都可以解释这些术语吗?
采纳答案by Sadique
A qualified name is one that has some sort of indication of where it belongs, e.g. a class specification, namespace specification, etc. An unqualified name is one that isn't qualified.
限定名称是具有某种指示其所属位置的名称,例如类规范、命名空间规范等。非限定名称是未限定的名称。
Read James McNellis' answer here:
在这里阅读 James McNellis 的回答:
What is a nested name specifier?
Given:
鉴于:
struct A {
struct B {
void F();
};
};
A
is an unqualified-id.::A
is a qualified-idbut has no nested-name-specifier.A::B
is a qualified-idandA::
is a nested-name-specifier.::A::B
is a qualified-idandA::
is a nested-name-specifier.A::B::F
is a qualified-idand bothB::
andA::B::
are nested-name-specifiers.::A::B::F
is a qualified-idand bothB::
andA::B::
are nested-name-specifiers.
A
是一个不合格的 id。::A
是一个qualified-id,但没有nested-name-specifier。A::B
是一个限定 ID并且A::
是一个嵌套名称说明符。::A::B
是一个限定 ID并且A::
是一个嵌套名称说明符。A::B::F
是一个qualified-id,B::
并且A::B::
都是nested-name-specifiers。::A::B::F
是一个qualified-id,B::
并且A::B::
都是nested-name-specifiers。
回答by Alok Save
A qualified name is one that specifies a scope.
Consider the following sample program, the references to cout
and endl
are qualified names:
限定名称是指定范围的名称。
考虑以下示例程序,对cout
和的引用endl
是限定名称:
#include <iostream>
int main()
{
std::cout<<"Hello world!"<<std::endl;
return 0;
}
Notice that the use of cout
and endl
began with std::
. These make them Qualified names.
请注意,使用cout
和endl
开始std::
。这些使他们成为合格的名称。
If we brought cout and endl into scope by a using declaration or directive*(such as using namespace std;
), and used just cout
and endl
just by themselves , they would have been unqualified names, because they would lack the std::
.
如果我们把cout和ENDL到范围通过using声明或指令*(如using namespace std;
),只是用来cout
和endl
自己只是,他们本来不合格的名字,因为他们缺乏std::
。