0

我来自 C 工作环境和 C++ 新手。请在以下声明中提供帮助。

在一些函数 foo() 中,我找到了这段代码。

::ifstream ifObj;

我知道它正在声明输入文件流对象。

但是我在这里完全不知道::范围解析的概念。这是什么以及为什么在对象声明中使用它。

无法在任何地方找到,因此问。

4

2 回答 2

2

专门针对您的问题,

就是让编译器在追踪 ifstream 类型来源的同时解决歧义。

ifstream 可以在诸如 boost 之类的 3rd 方库中声明以提供不同的含义,

考虑到你有,

namespace boost {
 typedef int ifstream;
}

using namespace boost;

//but here you want global ifsteam, not from boost, so
::ifstream ifObj; // Here you are creating a object for global ifstream, not for boost's ifstream,
于 2013-09-11T12:55:59.207 回答
1

如果该令牌在本地命名空间中被覆盖,则一元范围解析运算符用于引用该令牌的全局命名空间版本。

前:

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;
}
于 2013-09-11T12:55:41.223 回答