前面加上“::”是什么意思?在 C++ 中调用函数做什么?

发布于 2024-12-18 08:19:27 字数 353 浏览 0 评论 0原文

可能的重复:
前置双冒号“的含义是什么: :” 到类名?

我一直在查看遗留的 C++ 代码,它有这样的内容:

::putenv(local_tz_char);
::tzset();

在函数调用前面添加“::”的语法意味着什么? Google-fu 让我失望了。

Possible Duplicate:
What is the meaning of prepended double colon “::” to class name?

I have been looking at a legacy C++ code and it had something like this:

::putenv(local_tz_char);
::tzset();

What does this syntax of prepending "::" to the function calls mean? Google-fu is failing me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

云淡风轻 2024-12-25 08:19:27

这意味着编译器将在全局命名空间中查找函数 putenv()tzset()

示例

#include <iostream>

using namespace std;

//global function
void foo()
{
    cout << "This function will be called by bar()";
}

namespace lorem
{
    void foo()
    {
        cout << "This function will not be called by bar()";
    }

    void bar()
    {
        ::foo();
    }
}

int main()
{
    lorem::bar(); //will print "This function will be called by bar()"
    return 0;
}

It means that the functions putenv() and tzset() will be looked up by the compiler in the global namespace.

Example

#include <iostream>

using namespace std;

//global function
void foo()
{
    cout << "This function will be called by bar()";
}

namespace lorem
{
    void foo()
    {
        cout << "This function will not be called by bar()";
    }

    void bar()
    {
        ::foo();
    }
}

int main()
{
    lorem::bar(); //will print "This function will be called by bar()"
    return 0;
}
巷子口的你 2024-12-25 08:19:27

也称为范围解析运算符

在C++中用于定义已经声明的成员函数(在
特定类的扩展名为 .hpp 或 .h 的头文件。
在 .cpp 文件中,可以定义常用的全局函数或
类的成员函数。为了区分正常
函数和类的成员函数,需要使用
类名和类名之间的范围解析运算符 (::)
成员函数名称,即 Ship::foo(),其中 Ship 是一个类,而 foo()
是该类 Ship 的成员函数。

维基百科的例子:

#include <iostream>

// Without this using statement cout below would need to be std::cout
using namespace std; 

int n = 12; // A global variable

int main() {
  int n = 13; // A local variable
  cout << ::n << endl; // Print the global variable: 12
  cout << n   << endl; // Print the local variable: 13
}

Also known as Scope resolution operator

In C++ is used to define the already declared member functions (in the
header file with the .hpp or the .h extension) of a particular class.
In the .cpp file one can define the usual global functions or the
member functions of the class. To differentiate between the normal
functions and the member functions of the class, one needs to use the
scope resolution operator (::) in between the class name and the
member function name i.e. ship::foo() where ship is a class and foo()
is a member function of the class ship.

Example from Wikipedia:

#include <iostream>

// Without this using statement cout below would need to be std::cout
using namespace std; 

int n = 12; // A global variable

int main() {
  int n = 13; // A local variable
  cout << ::n << endl; // Print the global variable: 12
  cout << n   << endl; // Print the local variable: 13
}
情痴 2024-12-25 08:19:27

昨天(一年多)就类似的问题进行了讨论。也许您可以在这里找到更深入的答案。

前置双冒号的含义是什么: :“?

There was a discussion yesterday (+ a year) on a similar question. Perhaps you can find a more indepth answer here.

What is the meaning of prepended double colon "::"?

去了角落 2024-12-25 08:19:27

意思是:在全局命名空间中查找该函数。

It means: look up the function in the global namespace.

碍人泪离人颜 2024-12-25 08:19:27

它将使用特定的非限定名称(而不是使用 using 关键字导入的任何名称)。

It will use the specifically unqualified name (as opposed to anything imported with the using keyword).

倾城月光淡如水﹏ 2024-12-25 08:19:27

:: 是作用域解析运算符,它告诉编译器在什么作用域中查找该函数。

例如,如果您有一个带有局部变量 var 的函数,并且有一个同名的全局变量,则可以选择通过在前面添加作用域解析运算符来访问全局变量:

int var = 0;

void test() {
    int var = 5;
    cout << "Local: " << var << endl;
    cout << "Global: " << ::var << endl;
}

IBM C++ 编译器文档像这样说(来源):

::(范围解析)运算符用于限定隐藏名称,以便
您仍然可以使用它们。您可以使用一元作用域运算符,如果
命名空间范围或全局范围名称被显式隐藏
在块或类中声明同名。

对于类内部的方法和外部同名的版本也可以执行相同的操作。如果您想访问特定命名空间中的变量、函数或类,您可以像这样访问它: ::

但有一点需要注意,即使尽管它是一个运算符,但它不是可以重载的运算符之一。

the :: is the scope resolution operator, it tells the compiler in what scope to find the function.

For instance if you have a function with a local variable var and you have a global variable of the same name, you can choose to access the global one by prepending the scope resolution operator:

int var = 0;

void test() {
    int var = 5;
    cout << "Local: " << var << endl;
    cout << "Global: " << ::var << endl;
}

The IBM C++ compiler documentation puts it like this (source):

The :: (scope resolution) operator is used to qualify hidden names so
that you can still use them. You can use the unary scope operator if a
namespace scope or global scope name is hidden by an explicit
declaration of the same name in a block or class.

The same can be done for methods inside a class and versions of the same name outside. If you wanted to access a variable, function or class in a specific namespace you could access it like this: <namespace>::<variable|function|class>

One thing to note though, even though it is an operator it is not one of the operators that can be overloaded.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文