📄 page_927.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>page_927</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_926.html">< previous page</a></td> <td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_927</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_928.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 927</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="Times New Roman, Times, Serif" size="3"><i>The Slicing Problem</i></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">Our </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Print</font><font face="Times New Roman, Times, Serif" size="3"> function uses pass-by-value for the formal parameter </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">someTime</font><font face="Times New Roman, Times, Serif" size="3">. Pass-by-value sends a copy of the actual parameter to the formal parameter. Whenever you pass an object of a child class to an object of its parent class using pass-by-value, only the data members they have in common are copied. Remember that a child class is often larger than its parentthat is, it contains additional data members. For example, a </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Time</font><font face="Times New Roman, Times, Serif" size="3"> object has three data members(</font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">hrs</font><font face="Times New Roman, Times, Serif" size="3">, </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">mins</font><font face="Times New Roman, Times, Serif" size="3">, and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">secs</font><font face="Times New Roman, Times, Serif" size="3">), but an </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">ExtTime</font><font face="Times New Roman, Times, Serif" size="3"> object has four data members (</font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">hrs</font><font face="Times New Roman, Times, Serif" size="3">,</font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">mins</font><font face="Times New Roman, Times, Serif" size="3">,</font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">secs</font><font face="Times New Roman, Times, Serif" size="3">, and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">zone</font><font face="Times New Roman, Times, Serif" size="3">). When the larger class object is copied to the smaller formal parameter using pass-by-value, the extra data members are discarded or sliced off. This situation is called the <i>slicing problem</i> (see Figure 16-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">(The slicing problem also occurs with assignment operations. In the statement</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">parentClassObject聽=聽childClassObject;</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">only the data members that the two objects have in common are copied. Additional data members contained in </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">childClassObject</font><font face="Times New Roman, Times, Serif" size="3"> are not copied.)</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 pass-by-reference, the slicing problem does not occur because the <i>address</i> of the actual parameter is sent to the function. Let's change the heading of our </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Print</font><font face="Times New Roman, Times, Serif" size="3"> function so that </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">someTime</font><font face="Times New Roman, Times, Serif" size="3"> is a reference parameter:</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">void聽Print(聽/*聽in聽*/聽Time&聽someTime聽)</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">Now when we pass </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">endTime</font><font face="Times New Roman, Times, Serif" size="3"> as the actual parameter, its address is sent to the function. Its time zone member is not sliced off because no copying takes place. But to our dismay, the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Print</font><font face="Times New Roman, Times, Serif" size="3"> function <i>still</i> prints only three of </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">endTime</font><font face="Times New Roman, Times, Serif" size="3">'s data membershours, minutes, and seconds. Within the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Print</font><font face="Times New Roman, Times, Serif" size="3"> function, the difficulty is that static binding is used in the statement</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">someTime.Write();</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 compiler must generate machine language code for the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Print</font><font face="Times New Roman, Times, Serif" size="3"> function at compile time, but the type of the actual parameter (</font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Time</font><font face="Times New Roman, Times, Serif" size="3"> or </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">ExtTime</font><font face="Times New Roman, Times, Serif" size="3">) isn't known until run time. How can the compiler know which </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Write</font><font face="Times New Roman, Times, Serif" size="3"> function to use</font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Time::Write</font><font face="Times New Roman, Times, Serif" size="3"> or </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">ExtTime::Write</font><font face="Times New Roman, Times, Serif" size="3">? The compiler cannot know, so it uses </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Time::Write</font><font face="Times New Roman, Times, Serif" size="3"> because the formal parameter </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">someTime</font><font face="Times New Roman, Times, Serif" size="3"> is of type </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Time</font><font face="Times New Roman, Times, Serif" size="3">. Therefore, the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Print</font><font face="Times New Roman, Times, Serif" size="3"> function always prints just three valueshours, minutes, and secondsregardless of the type of the actual parameter. Fortunately, C++ provides a very simple solution to our problem: <i>virtual functions.</i></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_926.html">< previous page</a></td> <td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_927</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_928.html">next page ></a></td> </tr> </table> </body> </html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -