gpt4 book ai didi

c++ - 没有范围的范围解析运算符

转载 作者:IT老高 更新时间:2023-10-28 14:00:02 31 4
gpt4 key购买 nike

在 C++ 中,在没有作用域的情况下使用作用域解析运算符的目的是什么?例如:

::foo();

最佳答案

表示全局范围。当您在同一范围内有冲突的函数或变量并且您需要使用全局运算符时,您可能需要使用此运算符。你可能有类似的东西:

void bar();    // this is a global function

class foo {
void some_func() { ::bar(); } // this function is calling the global bar() and not the class version
void bar(); // this is a class member
};

如果您需要从类成员函数中调用全局 bar() 函数,您应该使用::bar() 来获取该函数的全局版本。

关于c++ - 没有范围的范围解析运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75213/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com