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. 1 #include<iostream>
  2. 2 using namespace std;
  3. 3
  4. 4 int x; // Global x
  5. 5
  6. 6 int main()
  7. 7 {
  8. 8 int x = 10; // Local x
  9. 9 cout<<"Value of global x is "<<::x<<endl;
  10. 10 cout<<"Value of local x is "<<x;
  11. 11 getchar();
  12. 12 return 0;
  13. 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. 1 void scope_resolution(void)
  2. 2 {
  3. 3 static unsigned int i = 2;
  4. 4 {
  5. 5 static unsigned int i = 1;
  6. 6 cout << ::i << endl; // i is looked at global scope
  7. 7 }
  8. 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. ZooKeeper 集群搭建 Error contacting service. It is probably not running.

    搭建环境:Centos 7 虚拟机 3台 按照此教程搭建:https://www.ilanni.com/?p=11393 之后出现错误:Error contacting service. It is ...

  2. 计算机网络漫谈之OSI七层模型和TCP/IP四层模型

    在 什么是网络? 中,你已经知道计算机网络是物理连接的"局域网"和工作于这个局域网上的"网络协议",并且我们的重心是网络协议.有关网络协议,按照目前的分层方式主 ...

  3. css盒模型简介

    如何了解盒模型 盒模型简介:盒模型是css布局的基石,它规定了网页元素如何显示以及元素间相互关系.css定义所有的元素都可以拥有像盒子一样的外形和平面空间. 盒模型的组成:内容区.补白/填充.边框.边 ...

  4. sqlalchemy delete object

    In SQL Alchemy you are deleting Objects that you get with a query from the database. This you can do ...

  5. kafka数据清理

    Kafka将数据持久化到了硬盘上,允许你配置一定的策略对数据清理,清理的策略有两个,删除和压缩. 数据清理的方式 删除 log.cleanup.policy=delete启用删除策略直接删除,删除后的 ...

  6. inux 下配置网卡的别名即网卡子IP的配置 转

    what 什么是ip别名?用windows的话说,就是为一个网卡配置多个ip.when 什么场合增加ip别名能派上用场?布网需要.多ip访问测试.特定软件对多ip的需要...and so on. ho ...

  7. centos7系列的网络yum源配置

    因为新安装centos机器yum比较旧,主要是对网易源进行配置,其它源也差不多.我是在securecrt远程ssh工具操作的,非虚拟机软件上. yum install lszrz -y   安装上传工 ...

  8. [loj6203]可持久化队列

    对于每一个节点,我们只需要知道他上len次插入(len是这个队列的元素个数)时所插入的元素就可以了 那么只需要将所有插入建为一棵树,然后找len次祖先就可以了,这个用倍增维护即可 还有一种比较神奇的做 ...

  9. 第09章 MySQL子查询

    第09章 MySQL子查询 子查询指一个查询语句嵌套在另一个查询语句内部的查询,这个特性从MySQL 4.1开始引入. SQL 中子查询的使用大大增强了 SELECT 查询的能力,因为很多时候查询需要 ...

  10. 将vue文档下载到本地预览

    1下载:https://github.com/vuejs/cn.vuejs.org   到本地 2. npm install npm start # 开发服务器地址为 http://localhost ...