⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 page_862.html

📁 Programming and Problem Solving with C++
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">	<html>		<head>			<title>page_862</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_861.html">&lt;&nbsp;previous page</a></td>				<td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_862</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_863.html">next page&nbsp;&gt;</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 862</font></td></tr></table><table cellspacing="0" border="0" width="528" cellpadding="4"><tr><td valign="top"><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"><i>M</i></font><i><font face="Times New Roman, Times, Serif" size="1">ATTERS OF STYLE</font></i></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table></td></tr><tr><td valign="top"><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">Avoiding Multiple Inclusion of Header Files</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table></td></tr><tr><td valign="top"><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">The TimeType specification file contains the preprocessor directive </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">#include聽bool.h</font><font face="Times New Roman, Times, Serif" size="3">. To avoid typing the lines</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table></td></tr><tr><td valign="top"><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">typedef聽int聽Boolean;<br />const聽Boolean聽TRUE聽=聽1;<br />const聽Boolean聽FALSE聽=聽0;</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table></td></tr><tr><td valign="top"><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 every file that uses Boolean data, we have said that it's useful to put these three lines into a file named </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">bool.h</font><font face="Times New Roman, Times, Serif" size="3"> and then </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">#include</font><font face="Times New Roman, Times, Serif" size="3"> the file whenever required. But think about what happens if a programmer using the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">TimeType</font><font face="Times New Roman, Times, Serif" size="3"> class already has included </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">bool.h</font><font face="Times New Roman, Times, Serif" size="3"> for other purposes, overlooking the fact that </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">timetype.h</font><font face="Times New Roman, Times, Serif" size="3"> also includes it:</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table></td></tr><tr><td valign="top"><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聽bool.h<br />#include聽timetype.h</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table></td></tr><tr><td valign="top"><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">The preprocessor inserts the file </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">bool.h</font><font face="Times New Roman, Times, Serif" size="3">, then </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">timetype.h</font><font face="Times New Roman, Times, Serif" size="3">, and then </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">bool.h</font><font face="Times New Roman, Times, Serif" size="3"> a second time (because </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">timetype.h</font><font face="Times New Roman, Times, Serif" size="3"> also includes </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">bool.h</font><font face="Times New Roman, Times, Serif" size="3">). The result is a compile-time error, because the identifiers TRUE and FALSE are defined twice.</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table></td></tr><tr><td valign="top"><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">The widely used solution to this problem is to write </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">bool.h</font><font face="Times New Roman, Times, Serif" size="3"> this way:</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table></td></tr><tr><td valign="top"><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">#ifndef聽BOOL_H<br />#define聽BOOL_H<br />typedef聽int聽Boolean;<br />const聽Boolean聽TRUE聽=聽1;<br />const聽Boolean聽FALSE聽=聽0;<br />#endif</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table></td></tr><tr><td valign="top"><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">The lines beginning with </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">#</font><font face="Times New Roman, Times, Serif" size="3"> are directives to the preprocessor. </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">BOOL_H</font><font face="Times New Roman, Times, Serif" size="3">(or any identifier you wish to use) is a preprocessor identifier, not a C++ program identifier. In effect, these directives say:</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table></td></tr><tr><td valign="top"><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">If the preprocessor identifier </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">BOOL_H</font><font face="Times New Roman, Times, Serif" size="3"> is not already defined, then:</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table></td></tr><tr><td valign="top"><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">1. define </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">BOOL_H</font><font face="Times New Roman, Times, Serif" size="3"> as an identifier known to the preprocessor, <i>and</i></font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table></td></tr><tr><td valign="top"><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">2. let the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">typedef</font><font face="Times New Roman, Times, Serif" size="3"> and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">const</font><font face="Times New Roman, Times, Serif" size="3"> declarations pass through to the compiler.</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table></td></tr><tr><td valign="top"><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">If a subsequent </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">#include</font><font face="Times New Roman, Times, Serif" size="3"></font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">bool.h</font><font face="Times New Roman, Times, Serif" size="3"> is encountered, the test </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">#ifndef聽BOOL_H</font><font face="Times New Roman, Times, Serif" size="3"> will fail. The </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">typedef</font><font face="Times New Roman, Times, Serif" size="3"> and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">const</font><font face="Times New Roman, Times, Serif" size="3"> declarations will not pass through to the compiler a second time.</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table></td></tr></table><br /></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_861.html">&lt;&nbsp;previous page</a></td>				<td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_862</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_863.html">next page&nbsp;&gt;</a></td>			</tr>		</table>		</body>	</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -