What#39;s the purpose of a leading quot;::quot; in a C++ method call(引导“::的目的是什么?在 C++ 方法调用中)
问题描述
我一直在使用 Boost 库,在 Boost.Exception 中,我注意到如下代码:
I've been using the Boost libraries, and in Boost.Exception, I've noticed code like the following:
#define BOOST_THROW_EXCEPTION(x) ::boost::throw_exception(x)
只是出于好奇:boost::throw_exception(x)之前的前导::的目的是什么?
Just out of curiosity: what is the purpose of the leading :: before boost::throw_exception(x)?
推荐答案
引用根命名空间.如果您的类或命名空间使用的名称也存在于根目录中,但在某些时候您希望引用根版本,这通常很有用.
To refer to the root namespace. This is often useful if your class or you namespace uses a name which also exists in the root, but at some point you wish to refer to the root version.
例如,如果我在类中重载了 new,但希望在某个时候引用默认(根)new,那么我将使用 ::new 来引用 root new.
For example, if I have overloaded new in my class, but wish at some point to refer to the default (root) new, then I would use ::new to refer to root new.
这篇关于引导“::"的目的是什么?在 C++ 方法调用中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:引导“::"的目的是什么?在 C++ 方法调用中
基础教程推荐
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- CString 到 char* 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
