📄 scope.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<link rel="stylesheet" href="css/stdlayout.css" type="text/css">
<link rel="stylesheet" href="css/print.css" type="text/css">
<meta content="text/html; charset=gb2312" http-equiv="content-type">
<title>变数可视范围(Scope)</title>
</head>
<body>
<h3><a href="http://caterpillar.onlyfun.net/GossipCN/index.html">From
Gossip@caterpillar</a></h3>
<h1><a href="CppGossip.html">C++
Gossip: 变数可视范围(Scope)</a></h1>
在C++中,谈到可视范围(scope)可分为许多层次,也可以谈到很复杂,在这边先谈谈“全域变数”(Global
variable)、“区域变数”(Local variable)与“区块变数”(Block variable)。 <br>
<br>
全域变数是指直接宣告在(主)函式之外的变数,这个变数在整个程式之中都“看”得它的存在,而可以呼叫使用,例如:<br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">const double PI =
3.14159; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">doule area(double
r) { </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
return r*r*PI; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">}</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int main() { </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> //
..... </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
return 0; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">} </span><br>
</div>
<br>
在这个例子中,PI这个变数可以被主函式main()与副函式area()来使用,通常全域变数是用来定义一些常数,初学者不应为了方便而将所有的变数都
设定为全域变数,否则将来一定会发生变数名称管理上的问题,全域变数的生命周期始于程式开始之时,终止于程式结束之时。 <br>
<br>
区域变数是指宣告在函式之内的变数,或是宣告在参数列之前的变数,它的可视范围只在宣告它的函式区块之中,其它的函式不可以使用该变数,例如在上例的主函
式中,您不可以直接对area()函式中的变数r作出存取的动作,区域变数的生命周期开始于函式被呼叫之后,终止于函式执行完毕之时。 <br>
<br>
区块变数是指宣告在某个陈述区块之中的变数,例如while回圈区块中,或是for回圈区块,例如下面的变数i在回圈结束之后,就会自动消失: <br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">for(int i = 0; i <
100; i++) </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> //
....</span><br>
</div>
<br>
当一可视范围大的变数与可视范围小的变数发生同名状况时,可视范围小的变数会暂时覆盖可视范围大的变数,称之为“变数覆盖”,例如: <br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int i = 10; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">for(int i = 0; i
< 100; i++) {</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> //
...</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">}</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">cout << i
<< endl;</span><br>
</div>
<br>
这个程式最后显示的 i 值仍是10,当执行回圈时,回圈内的 i 变数作用将覆盖回圈外的 i
变数;同样的作用发生于全域变数与区域变数发生同名的时候。 <br>
<br>
再来介绍static变数,当变数有宣告时加上static限定时,一但变数生成,它就会一直存在记忆体之中,即使函式执行完毕,变数也不会消失,例如:
<br>
<br>
<pre>#include <iostream> <br>using namespace std; <br><br>void count(); <br><br>int main() { <br><br> for(int i = 0; i < 10; i++) <br> count(); <br><br> return 0; <br>} <br><br>void count() { <br> static int c = 1; <br> cout << c << endl; <br> c++; <br>}</pre>
<br>
<span class="postbody">
执行结果:</span><br>
<table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td style="background-color: rgb(0, 0, 0);"><small><span style="color: rgb(255, 255, 255);">1<br>
2<br>
3<br>
4<br>
5<br>
6<br>
7<br>
8<br>
9<br>
10</span></small><span style="color: rgb(255, 255, 255);"><br>
</span></td>
</tr>
</tbody>
</table>
<br>
虽然变数c是在count()函式中宣告的,但是函式结束后,变数仍然存在,它会直到程式执行结束时才消失,虽然变数一直存在,但由于它是被宣告在函式之
中,所以函式之外仍无法存取static变数。 <br>
<br>
extern可以声明变数会在其它的位置被定义,这个位置可能是在同一份文件之中,或是在其它文件之中,例如:<br>
<ul>
<li>some.cpp</li>
</ul>
<pre>double someVar = 1000;<br>// ... 其它定义</pre>
<br>
<ul>
<li>main.cpp</li>
</ul>
<pre>#include <iostream> <br>using namespace std; <br><br>int main() { <br> extern double someVar;<br><br> cout << someVar << endl;<br> <br> return 0; <br>}</pre>
<br>
在main.cpp中实际上并没有宣告someVar,extern指出someVar是在其它位置被定义,编译器会试图在其它位置或文件中找出
someVar的定义,结果在some.cpp中找到,因而会显示结果为1000,要注意的是,extern声明someVar在其它位置被定义,如果您
在使用extern时同时指定其值,则视为在该位置定义变数,结果就引发重覆定义错误,例如:<br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">#include
<iostream> </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">using namespace
std; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int main() { </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
extern double someVar = 2000; // error, `someVar' has both `extern' and
initializer </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
...</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
return 0; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">} </span><br>
</div>
<br>
您必须先声明extern找到变数,再重新指定其值,这么使用才是正确的:<br>
<div style="margin-left: 40px; font-weight: bold; font-family: Courier New,Courier,monospace;">#include
<iostream> <br>
using namespace std; <br>
<br>
int main() { <br>
extern double someVar;<br>
someVar = 2000;<br>
...<br>
return 0; <br>
} <br>
</div>
<br>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -