Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我知道 C++ 中的 :: 运算符是范围解析,但是像这样在类中调用函数的目的是什么
class MyClass { int myFunc(int argument) { // do some stuff return (::myFunc(another_argument)); } }
有实际原因吗?是“这个”的意思吗?
如果您有这样的用例:
//in the global namespace int myFunc(int); //elsewhere class MyClass { int myFunc(int argument) { // do some stuff return (::myFunc(another_argument)); } }
这里我们需要区分成员函数和自由函数。这在包装 C 库时很常见。
在这种情况下::,强制编译选择驻留在全局命名空间中的版本,而不是最终会递归调用自身的成员函数。
::