📄 page_392.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>page_392</title> <link rel="stylesheet" href="reset.css" type="text/css" media="all"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <table summary="top nav" border="0" width="100%"> <tr> <td align="left" width="30%" style="background: #EEF3E2"><a style="color: blue; font-size: 120%; font-weight: bold; text-decoration: none; font-family: verdana;" href="page_391.html">< previous page</a></td> <td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_392</strong></td> <td align="right" width="30%" style="background: #EEF3E2"><a style="color: blue; font-size: 120%; font-weight: bold; text-decoration: none; font-family: verdana;" href="page_393.html">next page ></a></td> </tr> <tr> <td align="left" colspan="3" style="background: #ffffff; padding: 20px;"> <table border="0" width="100%" cellpadding="0"><tr><td align="center"> <table border="0" cellpadding="2" cellspacing="0" width="100%"><tr><td align="left"></td> <td align="right"></td> </tr></table></td></tr><tr><td align="left"><p></p><table border="0" cellspacing="0" cellpadding="0" width="100%"><tr><td align="right"><font face="Times New Roman, Times, Serif" size="2" color="#FF0000">Page 392</font></td></tr></table><table border="0" cellspacing="0" cellpadding="0" width="100%"><tr><td rowspan="5"><img src="3e26ecb1b6ac508ae10a0e39d2fb98b2.gif" border="0" width="96" height="1" alt="3e26ecb1b6ac508ae10a0e39d2fb98b2.gif" /></td> <td colspan="3" height="12"></td> <td rowspan="5"><img src="3e26ecb1b6ac508ae10a0e39d2fb98b2.gif" border="0" width="96" height="1" alt="3e26ecb1b6ac508ae10a0e39d2fb98b2.gif" /></td></tr><tr><td colspan="3" height="2"><table cellpadding="0" cellspacing="0" border="0"><tr><td></td></tr></table></td></tr><tr><td></td> <td><font face="Times New Roman, Times, Serif" size="2">Name Precedence The precedence that a local identifier in a function has over a global identifier with the same name in any references that the function makes to that identifier; also called <i>name hiding.</i></font></td><td></td></tr><tr><td colspan="3" height="2"><table cellpadding="0" cellspacing="0" border="0"><tr><td></td></tr></table></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Times New Roman, Times, Serif" size="3">Here's an example that uses both local and global declarations:</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Courier New, Courier, Mono New, Courier, Mono" size="2">#include聽<iostream.h><br /><br />void聽SomeFunc(聽float聽);<br /><br />const聽int聽a聽=聽17;聽聽聽//聽A聽global聽constant<br />int聽b;聽聽聽聽聽聽聽聽聽聽聽聽聽聽//聽A聽global聽variable<br />int聽c;聽聽聽聽聽聽聽聽聽聽聽聽聽聽//聽Another聽global聽variable<br /><br />int聽main()<br />{<br />聽聽聽聽b聽=聽4;聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽//聽Assignment聽to聽global聽b<br />聽聽聽聽c聽=聽6;聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽//聽Assignment聽to聽global聽c<br />聽聽聽聽SomeFunc(42.8);<br />聽聽聽聽return聽0;<br />}<br /><br />void聽SomeFunc(聽float聽c聽)聽聽聽聽聽聽聽聽//聽Prevents聽access聽to聽global聽c<br />{<br />聽聽聽聽float聽b;聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽//聽Prevents聽access聽to聽global聽b<br /><br />聽聽聽聽b聽=聽2.3;聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽//聽Assignment聽to聽local聽b<br />聽聽聽聽cout聽<聽a聽=聽聽<聽a;聽聽聽聽聽聽聽//聽Output聽global聽a聽(17)<br />聽聽聽聽cout聽<聽聽b聽=聽聽<聽b;聽聽聽聽聽//聽Output聽local聽b聽(2.3)<br />聽聽聽聽cout聽<聽聽c聽=聽聽<聽c;聽聽聽聽聽//聽Output聽local聽c聽(42.8)<br />}</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Times New Roman, Times, Serif" size="3">In this example, function </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">SomeFunc</font><font face="Times New Roman, Times, Serif" size="3"> accesses global constant a but declares its own local variables </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">b</font><font face="Times New Roman, Times, Serif" size="3"> and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">c</font><font face="Times New Roman, Times, Serif" size="3">. Thus, the output would be</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Courier New, Courier, Mono New, Courier, Mono" size="2">a聽=聽17聽b聽=聽2.3聽c聽=聽42.8</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Times New Roman, Times, Serif" size="3">Local variable </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">b</font><font face="Times New Roman, Times, Serif" size="3"> takes precedence over global variable </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">b</font><font face="Times New Roman, Times, Serif" size="3">, effectively hiding global </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">b</font><font face="Times New Roman, Times, Serif" size="3"> from the statements in function </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">SomeFunc</font><font face="Times New Roman, Times, Serif" size="3">. Formal parameter </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">c</font><font face="Times New Roman, Times, Serif" size="3"> also blocks access to global variable </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">c</font><font face="Times New Roman, Times, Serif" size="3"> from within the function. Formal parameters act just like local variables in this respect.</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table></td></tr></table><p><font size="0"></font></p>聽 </td> </tr> <tr> <td align="left" width="30%" style="background: #EEF3E2"><a style="color: blue; font-size: 120%; font-weight: bold; text-decoration: none; font-family: verdana;" href="page_391.html">< previous page</a></td> <td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_392</strong></td> <td align="right" width="30%" style="background: #EEF3E2"><a style="color: blue; font-size: 120%; font-weight: bold; text-decoration: none; font-family: verdana;" href="page_393.html">next page ></a></td> </tr> </table> </body> </html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -