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?的更多相关文章

  1. Global & Local Variable in Python

    Following code explain how 'global' works in the distinction of global variable and local variable. ...

  2. python 错误--UnboundLocalError: local variable '**' referenced before assignment

    val = 9 def test(flag): if flag: val = 1 else: print("test") return val if __name__ == '__ ...

  3. local variable 'xxx' referenced before assignment

    这个问题很囧,在外面定义了一个变量 xxx ,然后在python的一个函数或类里面引用这个变量,并改变它的值,结果报错local variable 'xxx' referenced before as ...

  4. python的UnboundLocalError: local variable 'xxx' referenced b

    一.意思: 本地变量xxx引用前没定义. 二.错误原因     在于python没有变量的声明 , 所以它通过一个简单的规则找出变量的范围 :如果有一个函数内部的变量赋值 ,该变量被认为是本地的,所以 ...

  5. Python问题:UnboundLocalError: local variable 'xxx' referenced before assignment

    参考链接: http://blog.csdn.net/onlyanyz/article/details/45009697 https://www.cnblogs.com/fendou-999/p/38 ...

  6. 洗礼灵魂,修炼python(23)--自定义函数(4)—闭包进阶问题—>报错UnboundLocalError: local variable 'x' referenced before assignment

    闭包(lexical closure) 什么是闭包前面已经说过了,但是由于遗留问题,所以单独作为一个章节详解讲解下 不多说,看例子: def funx(x): def funy(y): return ...

  7. 常见的local variable 'x' referenced before assignment问题

    def fun1(): x = 5 def fun2(): x *= 2 return x return fun2() 如上代码,调用fun1() 运行会出错:UnboundLocalError: l ...

  8. [合集]解决Python报错:local variable 'xxx' referenced before assignment

    a = 1 def use(): print(a) #输出1 引用不会报错 a = 1 def use(): a = 3 print(a) #输出 3 重新赋值也不会报错. 局部变量会优先在函数内部去 ...

  9. Cannot refer to the non-final local variable user defined in an enclosing scope 内部类定义在方法内,方法定义的参数(形参)无法被内部类直接访问,需要用final定义

    为什么匿名内部类参数必须为final类型 1)  从程序设计语言的理论上:局部内部类(即:定义在方法中的内部类),由于本身就是在方法内部(可出现在形式参数定义处或者方法体处),因而访问方法中的局部变量 ...

随机推荐

  1. idea断点调试

    基本使用 1 show execution point (Alt+F10):跳转到断点所执行的地方,也就是说你在看代码的时候,点到其他地方,一点这个按钮,就到了程序执行到当前哪行的代码的地方. 2 s ...

  2. K8S 部署 SpringBoot 项目(一篇够用)

    现在比较多的互联网公司都在尝试将微服务迁到云上,这样的能够通过一些成熟的云容器管理平台更为方便地管理微服务集群,从而提高微服务的稳定性,同时也能较好地提升团队开发效率. 但是迁云存在一定的技术难点,今 ...

  3. go 集合

    p.p1 { margin: 0; font: 12px ".PingFang SC"; color: rgba(69, 69, 69, 1) } span.s1 { font: ...

  4. 大一C语言学习笔记(8)---指针篇--动态内存是什么?与静态内存有什么区别?怎么使用动态内存,有什么需要注意的地方?

    静态内存指的是在编译时系统自动给其分配的内存,运行结束后会自动释放:静态内存是在栈中分配的: 动态内存是我们程序员手动分配的内存,正常情况下,程序运行结束后,也不会自动释放,所以为了避免发生未知的错误 ...

  5. c++学习笔记(十)

    返回应用类型 返回引用 1.不要返回局部变量的引用 为了验证为什么不能返回局部变量的引用,我按照所学的例题自己做了一点小测试. #include<iostream> using names ...

  6. pytest框架+conftest.py配置公共数据的准备和清理

    1.pytest介绍:1.自动发现测试模块和测试方法 2.断言使用 assert+表达式即可 3.可以设置会话级.模块级.类级.函数级的fixture 数据准备+清理工作 4.丰富的插件库,==all ...

  7. vue项目在打包时,去掉所有的console.log输出

    npm i babel-plugin-transform-remove-console -S const proPlugins = [];// 开发环境 不做操作 // 生产环境,去掉console ...

  8. 这可能是你看过最详细的NodeJS安装配置教程

    博主是一枚Java菜鸡,今天在B站上看一些教程视频的时候偶尔看了一眼评论区,发现好多人在Node和Vue安装的位置卡住了,便决定今晚肝出一套最详细的NodeJS安装配置的教程 本文适合初次接触Node ...

  9. [atAGC054F]Decrement

    令$a_{i}$和$b_{i}$分别为$A_{i}$和$B_{i}$减少的值,考虑判定$\{a_{i}\},\{b_{i}\}$能否被得到 结论:$\{a_{i}\},\{b_{i}\}$能否被得到当 ...

  10. salesforce零基础学习(一百零九)Lightning Login启用以及配置

    本篇参考:https://help.salesforce.com/s/articleView?id=sf.security_ll_overview.htm&type=5 我们在之前的篇中提到过 ...