DVK*_*DVK 7 c++ syntax namespaces
可能重复:
前缀双冒号"::"对类名的含义是什么?
我一直在寻找遗留的C++代码,它有这样的东西:
::putenv(local_tz_char);
::tzset();
Run Code Online (Sandbox Code Playgroud)
在函数调用中添加"::"的语法是什么意思?Google-fu让我失望了.
也称为Scope解析运算符
在C++中,用于定义特定类的已声明的成员函数(在带有.hpp或.h扩展名的头文件中).在.cpp文件中,可以定义通常的全局函数或类的成员函数.要区分正常函数和类的成员函数,需要在类名和成员函数名之间使用范围解析运算符(::),即ship :: foo()其中ship是类和foo ()是类船的成员函数.
来自维基百科的示例:
#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
}
Run Code Online (Sandbox Code Playgroud)
这意味着功能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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1042 次 |
| 最近记录: |