📄 page_995.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>page_995</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_994.html">< previous page</a></td> <td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_995</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_996.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 995</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="3">{<br /></font><font face="Times New Roman, Times, Serif" size="3"></font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Date聽conf(1,聽30,聽1998,聽Conference);</font><font face="Times New Roman, Times, Serif" size="3"></font><font face="Symbol" size="3">录</font><font face="Times New Roman, Times, Serif" size="3"> Constructor is invoked here<br /></font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">.<br /></font><font face="Times New Roman, Times, Serif" size="3"></font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">.<br /></font><font face="Times New Roman, Times, Serif" size="3"></font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">.<br />}</font><font face="Times New Roman, Times, Serif" size="3"></font><font face="Symbol" size="3">录</font><font face="Times New Roman, Times, Serif" size="3"> Destructor is invoked here because </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">conf</font><font face="Times New Roman, Times, Serif" size="3"> goes out of scope<br /></font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">}</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 the implementation file </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">date.cpp</font><font face="Times New Roman, Times, Serif" size="3">, the implementation of the class destructor is very simple:</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">Date::~Date()<br /><br />//聽Destructor<br /><br />//聽Postcondition:<br />//聽聽聽聽聽Array聽pointed聽to聽by聽msg聽is聽no聽longer聽on聽the聽free聽store<br />{<br />聽聽聽聽delete聽[]聽msg;<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">You cannot pass parameters to a destructor and, as with a class constructor, you must not declare the data type of the function.</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">Until now, we have not needed class destructors. In all previous examples of classes, the private data have been enclosed entirely within the abstraction barrier of the class. For example, in Chapter 15, a </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">TimeType</font><font face="Times New Roman, Times, Serif" size="3"> class object encapsulates all of its data:</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"><img src="46bd19fa3eb438197315ada511fd5da0.gif" border="0" alt="0995-01.gif" width="103" height="91" /></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">When </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">startTime</font><font face="Times New Roman, Times, Serif" size="3"> goes out of scope, destruction of </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">startTime</font><font face="Times New Roman, Times, Serif" size="3"> implies destruction of all of its component data.</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">With the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Date</font><font face="Times New Roman, Times, Serif" size="3"> class, some of the data are enclosed within the abstraction barrier and some of the data are not (Figure 17-9). Without the destructor function </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">~Date</font><font face="Times New Roman, Times, Serif" size="3">, destruction of a class object would deallocate <i>pointer</i> to the dynamic array, but would not deallocate the array itself. The result would be a memory leak; the dynamic array would remain allocated but no longer accessible.</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_994.html">< previous page</a></td> <td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_995</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_996.html">next page ></a></td> </tr> </table> </body> </html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -