Can we access global variable if there is a local variable with same name?
In C, we cannot access a global variable if we have a local variable with same name, but it is possible in C++ using scope resolution operator (::).
1 #include<iostream>
2 using namespace std;
3
4 int x; // Global x
5
6 int main()
7 {
8 int x = 10; // Local x
9 cout<<"Value of global x is "<<::x<<endl;
10 cout<<"Value of local x is "<<x;
11 getchar();
12 return 0;
13 }
the C language doesn't have concept of 'resolving scope'. Even C++ can't resolve anonymous scope variables. In C++, since the basic abstraction and encapsulation mechanism (class) can also introduce scope and class variables are possible, a special operator for resolving scope is required. Also, in C++, we can't access function scope variables, nor file scope variables in another file.
For example, see
1 void scope_resolution(void)
2 {
3 static unsigned int i = 2;
4 {
5 static unsigned int i = 1;
6 cout << ::i << endl; // i is looked at global scope
7 }
8 }
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
转载请注明:http://www.cnblogs.com/iloveyouforever/
2013-11-27 12:17:49
Can we access global variable if there is a local variable with same name?的更多相关文章
- Global & Local Variable in Python
Following code explain how 'global' works in the distinction of global variable and local variable. ...
- python 错误--UnboundLocalError: local variable '**' referenced before assignment
val = 9 def test(flag): if flag: val = 1 else: print("test") return val if __name__ == '__ ...
- local variable 'xxx' referenced before assignment
这个问题很囧,在外面定义了一个变量 xxx ,然后在python的一个函数或类里面引用这个变量,并改变它的值,结果报错local variable 'xxx' referenced before as ...
- python的UnboundLocalError: local variable 'xxx' referenced b
一.意思: 本地变量xxx引用前没定义. 二.错误原因 在于python没有变量的声明 , 所以它通过一个简单的规则找出变量的范围 :如果有一个函数内部的变量赋值 ,该变量被认为是本地的,所以 ...
- Python问题:UnboundLocalError: local variable 'xxx' referenced before assignment
参考链接: http://blog.csdn.net/onlyanyz/article/details/45009697 https://www.cnblogs.com/fendou-999/p/38 ...
- 洗礼灵魂,修炼python(23)--自定义函数(4)—闭包进阶问题—>报错UnboundLocalError: local variable 'x' referenced before assignment
闭包(lexical closure) 什么是闭包前面已经说过了,但是由于遗留问题,所以单独作为一个章节详解讲解下 不多说,看例子: def funx(x): def funy(y): return ...
- 常见的local variable 'x' referenced before assignment问题
def fun1(): x = 5 def fun2(): x *= 2 return x return fun2() 如上代码,调用fun1() 运行会出错:UnboundLocalError: l ...
- [合集]解决Python报错:local variable 'xxx' referenced before assignment
a = 1 def use(): print(a) #输出1 引用不会报错 a = 1 def use(): a = 3 print(a) #输出 3 重新赋值也不会报错. 局部变量会优先在函数内部去 ...
- Cannot refer to the non-final local variable user defined in an enclosing scope 内部类定义在方法内,方法定义的参数(形参)无法被内部类直接访问,需要用final定义
为什么匿名内部类参数必须为final类型 1) 从程序设计语言的理论上:局部内部类(即:定义在方法中的内部类),由于本身就是在方法内部(可出现在形式参数定义处或者方法体处),因而访问方法中的局部变量 ...
随机推荐
- 怎么将本地已有的一个项目上传到新建的git仓库的方法
将本地已有的一个非git项目上传到新建的git仓库的方法一共有两种. 一. 克隆+拷贝 第一种方法比较简单,直接用把远程仓库拉到本地,然后再把自己本地的项目拷贝到仓库中去.然后push到远程仓库上去即 ...
- Java经典面试题(二)-不古出品
@ 目录 1. 为什么说 Java 语言"编译与解释并存"? 2.Oracle JDK 和 OpenJDK 的对比? 3.字符型常量和字符串常量的区别? 4.Java 泛型了解么? ...
- Calendar.set方法获取前一天的当前时刻
获取前几天的当前时刻的时间方法 Calendar cal = Calendar.getInstance(); Date date = new Date();// 获取当前时间 cal.setTime( ...
- [前端随笔][Vue] 多级菜单实现思路——组件嵌套
说在前面 本篇记录学习了vue-element-admin中的多级菜单的实现 [传送门] @vue/cli 4.2.2:vuex:scss:组件嵌套 正文 创建项目 npm create 项目名 // ...
- Python基础(list与tuple)
#list 类似于数组的概念 classmates = ['傻狗1','傻狗2','傻狗3'] # print(classmates) # print(len(classmates)) # print ...
- IDEA maven Run Maven 启动方式
首先想要使用 Run Maven 启动需要在IDEA设置里找到plugins 在plugins窗口下面找到Browse Repositories 打开Browse Repositories 下载 ...
- 仿Word的支持横轴竖轴的WPF 标尺
最近在 https://mp.weixin.qq.com/s/3dEO0NZQv5YLqK72atG4Wg 官方公众号看到了 用WPF 制作 标尺 在去年项目上也接到了一个需求,用于排版自定义拖 ...
- python实现图像二值化
1.什么是图像二值化 彩色图像: 有blue,green,red三个通道,取值范围均为0-255 灰度图:只有一个通道0-255,所以一共有256种颜色 二值图像:只有两种颜色,黑色和白色,二值化就是 ...
- [atARC105D]Let's Play Nim
先对$n$分奇偶两种情况考虑-- $n$为奇数,显然先手希望最终产生的$x_{1}\oplus x_{2}\oplus...\oplus x_{n}=0$ 对于后手,考虑构造:将最大的未被选择的$a_ ...
- Go语言核心36讲(Go语言实战与应用十二)--学习笔记
34 | 并发安全字典sync.Map (上) 我们今天再来讲一个并发安全的高级数据结构:sync.Map.众所周知,Go 语言自带的字典类型map并不是并发安全的. 前导知识:并发安全字典诞生史 换 ...