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

📄 page_1080.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_1080</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_1079.html">&lt;&nbsp;previous page</a></td>				<td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_1080</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_1081.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 1080</font></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">聽聽聽聽while聽(聽!IsEmpty()聽)<br /><br />聽聽聽聽聽聽聽聽聽聽聽聽//聽Invariant聽(prior聽to聽test):<br />聽聽聽聽聽聽聽聽聽聽聽聽//聽聽聽聽聽All聽nodes聽before聽current聽first聽node聽in聽list<br />聽聽聽聽聽聽聽聽聽聽聽聽//聽聽聽聽聽have聽been聽deallocated<br /><br />聽聽聽聽聽聽聽聽DeleteTop(temp);<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">The copy-constructor is harder to write. Before we look at it, we must stress the importance of providing a copy-constructor whenever we also provide a destructor. Pretend that </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">OrdList</font><font face="Times New Roman, Times, Serif" size="3"> doesn't have a copy-constructor, and suppose that a client passes a class object to a function using pass-by-value. (Remember that passing a parameter by value sends a <i>copy</i> of the value of the actual parameter to the function.) Within the function, the formal parameter is initialized to be a copy of the caller's class object, including the caller's value of the private variable </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">head</font><font face="Times New Roman, Times, Serif" size="3">. At this point, both the actual parameter and the formal parameter are pointing to the same dynamic linked list. When the client function returns, the class destructor is invoked for the formal parameter, destroying the only copy of the linked list. Upon return from the function, the caller's linked list has disappeared!</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">By providing a copy-constructor, we ensure deep copying of an actual parameter to a formal parameter whenever pass-by-value occurs. The implementation of the copy-constructor, shown below, employs a commonly used algorithm for creating a new linked list as a copy of another.</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">OrdList::OrdList(聽const聽OrdList&amp;聽otherList聽)<br /><br />//聽Copy-constructor<br /><br />//聽Postcondition:<br />//聽聽聽聽聽IF聽otherList.head聽==聽NULL聽(i.e.,聽the聽other聽list聽is聽empty)<br />//聽聽聽聽聽聽聽聽聽head聽==聽NULL<br />//聽聽聽聽聽ELSE<br />//聽聽聽聽聽聽聽聽聽head聽points聽to聽a聽new聽linked聽list聽that聽is聽a聽copy聽of<br />//聽聽聽聽聽聽聽聽聽the聽linked聽list聽pointed聽to聽by聽otherList.head<br /><br />{<br />聽聽聽聽NodePtr聽fromPtr;聽聽聽聽//聽Pointer聽into聽list聽being聽copied聽from<br />聽聽聽聽NodePtr聽toPtr;聽聽聽聽聽聽//聽Pointer聽into聽new聽list聽being聽built<br /><br />聽聽聽聽if聽(otherList.head聽==聽NULL)<br />聽聽聽聽{<br />聽聽聽聽聽聽聽聽head聽=聽NULL;<br />聽聽聽聽聽聽聽聽return;<br />聽聽聽聽}<br /></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_1079.html">&lt;&nbsp;previous page</a></td>				<td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_1080</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_1081.html">next page&nbsp;&gt;</a></td>			</tr>		</table>		</body>	</html>

⌨️ 快捷键说明

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