::的作用非常简单,就是当局部变量和全局变量名字重叠的时候,指定某变量一定是来自全局变量。
#include<iostream>
using namespace std;
int result = 100;
int main(){
int result = 10;
if(true){
int result = 1;
cout << ::result;
}
system("pause");
return 0;
}其输出结果是100
C++中::作用符详解
本文详细介绍了C++中的作用域解析运算符(::)的使用方法及其应用场景。通过一个具体的例子展示了如何在局部变量与全局变量名称冲突时,利用::来明确指出使用的是全局变量。
::的作用非常简单,就是当局部变量和全局变量名字重叠的时候,指定某变量一定是来自全局变量。
#include<iostream>
using namespace std;
int result = 100;
int main(){
int result = 10;
if(true){
int result = 1;
cout << ::result;
}
system("pause");
return 0;
}
1918
998
856

被折叠的 条评论
为什么被折叠?