简体   繁体   English

没有范围的范围解析运算符

[英]scope resolution operator without a scope

In C++, what is the purpose of the scope resolution operator when used without a scope? 在C ++中,不带范围使用范围解析运算符的目的是什么? For instance: 例如:

::foo();

It means global scope. 这意味着全球范围。 You might need to use this operator when you have conflicting functions or variables in the same scope and you need to use a global one. 如果在同一作用域中有冲突的函数或变量,并且需要使用全局运算符,则可能需要使用此运算符。 You might have something like: 您可能会有类似以下内容:

void bar();    // this is a global function

class foo {
    void some_func() { ::bar(); }    // this function is calling the global bar() and not the class version
    void bar();                      // this is a class member
};

If you need to call the global bar() function from within a class member function, you should use ::bar() to get to the global version of the function. 如果需要从类成员函数中调用全局bar()函数,则应使用:: bar()来获取函数的全局版本。

Also you should note, that name resolution happens before overload resolution. 还应注意,名称解析发生在重载解析之前。 So if there is something with the same name in your current scope then it will stop looking for other names and try to use them. 因此,如果您当前的作用域中存在具有相同名称的名称,那么它将停止寻找其他名称并尝试使用它们。

void bar() {};
class foo {
    void bar(int) {};
    void foobar() { bar(); } // won't compile needs ::bar()
    void foobar(int i) { bar(i); } // ok
}

A name that begins with the scope resolution operator ( :: ) is looked up in the global namespace. 在全局名称空间中查找以范围解析运算符:: :)开头的名称。 We can see this by looking at the draft C++ standard section 3.4.3 Qualified name lookup paragraph 4 which says ( emphasis mine ): 通过查看C ++标准草案3.4.3节“ 合格名称查找”4段可以看到这一点,其中( 强调我的 ):

A name prefixed by the unary scope operator :: (5.1) is looked up in global scope , in the translation unit where it is used. 由一元作用域运算符:::(5.1)前缀的名称是在全局作用域中使用的翻译单位中查找的 The name shall be declared in global namespace scope or shall be a name whose declaration is visible in global scope because of a using-directive (3.4.3.2). 该名称应在全局名称空间范围内声明,或者是由于使用指令(3.4.3.2)而在全局范围内可见的名称。 The use of :: allows a global name to be referred to even if its identifier has been hidden (3.3.10). 使用:: 允许引用全局名称,即使其标识符已隐藏 (3.3.10)。

As the standard states this allows us to use names from the global namespace that would otherwise be hidden , the example from the linked document is as follows: 按照标准,这允许我们使用全局名称空间中的名称,否则它们将被隐藏 ,链接文档中的示例如下:

int count = 0;

int main(void) {
  int count = 0;
  ::count = 1;  // set global count to 1
  count = 2;    // set local count to 2
  return 0;
}

The wording is very similar going back to N1804 which is the earliest draft standard available. 措辞与N1804非常相似,后者是最早的标准草案。

如果在本地作用域中已经有一个名为foo()的函数,但是您需要在全局作用域中访问该函数。

My c++ is rusty but I believe if you have a function declared in the local scope, such as foo() and one at global scope, foo() refers to the local one. 我的c ++生锈了,但是我相信,如果您有一个在局部范围内声明的函数,例如foo()和一个在全局范围内声明的函数,则foo()则指的是局部函数。 ::foo() will refer to the global one. :: foo()将引用全局变量。

指全球范围

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

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