编译器默认选择的名字可能不是我们要用的名字
作用域解析运算符可以解决 默认选择的名字不是我们要用的名字的问题
例如有一个结构a 我们希望成员函数使用全局标识a
但编译器选择另外一个标识符,要告诉编译器你使用哪个标志服
作用域解析运算符可以解决 默认选择的名字不是我们要用的名字的问题
例如有一个结构a 我们希望成员函数使用全局标识a
但编译器选择另外一个标识符,要告诉编译器你使用哪个标志服
变量和全局作用域解析的例子
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
// Global scope resolution
int a;
void f() {}
struct S {
int a;
void f();
};
void S::f() {
::f(); // Would be recursive otherwise!
::a++; // Select the global a
a--; // The a at struct scope
}
int main() { S s; f(); } ///:~
如果 ::f() 没有作用域解析运算符,编译器会默认选择f(),和a
无输出