Static and Local Scope

Someone says, " I just want to define a int var in a .h file. Who pa Who?

Now we see the following example.

Static: Local in YOU

---------------ah.h-----------------

static int a=1;

------------------------------

----------------callseta.cpp--------------

#include"ah.h"

#include<iostream>

using namespace std;

void seta()

{

  a=2;

  //cout<<"call seta()"<<endl;

  //cout<<"in call, a="<<a<<endl;

}

--------------------------------------------

-----------------main.cpp---------------

#include"ah.h"

#include<iostream>

extern void seta();

using namespace std;

void main()

{

  cout<<"a="<<a<<endl;

  seta();

  cout<<"after seta()"<<endl;

  cout<<"a="<<a<<endl;

}

-----------------------------------

output  :

a=1
call seta()
in call, a=2
after seta()
a=1
请按任意键继续. . .

----------------------------------

this a is local in main.cpp. For include" ", the a is static.

So there are two "int a", one in main.cpp‘s scope or workspace, the other in callseta.cpp‘s scope.

They are two different int var, with different address allocated.

They only live in their own world. Any guy outside can not see her beauty.

Interesting things are:

when you call seta() in main.cpp, the pointer turns to the program space of callseta.cpp.

It is not realized that firstly clone the program to the main.cpp ‘s program data segament.

The pointer turns onto the orginal and the only one program data segament of seta() in callseta.cpp.

So in that scope, a means his a, not the a in main, which is amazing.

时间: 2024-08-08 22:01:39

Static and Local Scope的相关文章

PHP中Global和Local范围以及Static变量

1. Local scope function update_counter() { $counter++;//此处$counter为局部变量,与函数外的$counter非同一个 } $counter = 10; update_counter(); echo $counter; //输出:10 2. Global scope function update_counter() { global $counter;//利用global关键字在函数内进行声明即可获取全局域的$counter $cou

angularjs中directive声明scope对象时修饰符解释和用法

在angular中我们定义directive方法时,可以看到 return { restrict: 'AE', scope: {}, template: '<div></div>', link: function() {} } 除了代码中出现的属性,还有一些其他的属性可供配置,这里不作详述. 今天我们要说的重点是scope字段. 常规用法设置 scope: { name: '=', age: '=', sex: '@', say: '&' } 假设我们的hml代码如下 &l

Local Functions

int BOOST_LOCAL_FUNCTION(int x, int y) { // Local function. return x + y; } BOOST_LOCAL_FUNCTION_NAME(add) BOOST_TEST(add(1, 2) == 3); // Local function call. int BOOST_LOCAL_FUNCTION(void) { // No parameter. return 10; } BOOST_LOCAL_FUNCTION_NAME(te

你一直想知道的关于JavaScript scope的一切

对于一个JavaScript初学者(甚至是有经验的JavaScript开发者)而言,JavaScript语言中关于"域"(scope)的一些概念并不是那么直白或是容易理解的. 由此,这篇文章旨在帮助那些在听说过诸如域(scope),闭包(closure),关键字this,命名空间(namespace),函数域(function scope),全局域(global scope),词法作用域(lexical scope)以及公共域和私有域(public/private scope)等词汇后

angularjs于directive声明scope说明何时以及如何使用对象修饰符

于angular我们定义directive方法.查看 return { restrict: 'AE', scope: {}, template: '<div></div>', link: function() {} } 除了代码中出现的属性,另一些其它的属性可供配置,这里不作详述. 今天我们要说的重点是scope字段. 常规使用方法设置 scope: { name: '=', age: '=', sex: '@', say: '&' } 如果我们的hml代码例如以下 <

php Late Static Bindings延迟静态绑定

官网说道: As of PHP 5.3.0, PHP implements a feature called late static bindings which can be used to reference the called class in a context of static inheritance. More precisely, late static bindings work by storing the class named in the last "non-forw

PHP之static静态变量详解(二)

在看别人项目过程中,看到函数里面很多static修饰的变量,关于static修饰的变量,作用域,用法越看越困惑,所以查了下资料. static用法如下: 1.static 放在函数内部修饰变量 2.static放在类里修饰属性,或方法 3.static放在类的方法里修饰变量 4.static修饰在全局作用域的变量 所表示的不同含义如下: 1.在函数执行完后,变量值仍然保存 如下所示: <?php function testStatic() { static $val = 1; echo $val

[译] 你该知道的javascript作用域 (javascript scope)(转)

javascript有一些对于初学者甚至是有经验的开发者都难以理解的概念. 这个部分是针对那些听到 : 作用域, 闭包, this, 命名空间, 函数作用域, 函数作用域, 全局作用域, 变量作用域(后续翻译这个词我也没太懂), 公共/私有 作用域 后想要深入了解的人的. 当你看完这篇文章后你将了解有关以下问题的答案: 什么是作用域 什么是全局/局部 作用域 什么是命名空间,他和作用域的不同 this是什么,它是怎样被作用域影响的 什么是函数/lexical 作用域 什么是闭包 什么是公共/私有

[this] scope

Global Scope In a browser, the global scope is the window object. if in your code you simply have: var x = 9; You're actually setting the property window.x to 9 (when working in a browser). You could type window.x = 9; Local Scope JavaScript scopes a