📄 page_972.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>page_972</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_971.html">< previous page</a></td> <td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_972</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_973.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 972</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">*intPtr聽=聽28;<br />cout聽<聽intPtr聽<聽endl;<br />cout聽<聽*intPtr聽<聽endl;</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">then the output is</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">5008<br />28</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 first output statement prints the contents of </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">intPtr</font><font face="Times New Roman, Times, Serif" size="3"> (5008); the second prints the contents of the variable pointed to by </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">intPtr</font><font face="Times New Roman, Times, Serif" size="3"> (28).</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">Let's look at a more involved example of declaring pointers, taking addresses, and dereferencing pointers. The following program fragment declares several types and variables. In this code, 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 is the C++ class we developed in Chapter 15 with member functions </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Set</font><font face="Times New Roman, Times, Serif" size="3">, </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Increment</font><font face="Times New Roman, Times, Serif" size="3">, </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Write</font><font face="Times New Roman, Times, Serif" size="3">, </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Equal</font><font face="Times New Roman, Times, Serif" size="3">, and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">LessThan</font><font face="Times New Roman, Times, Serif" 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="Courier New, Courier, Mono New, Courier, Mono" size="2">#include聽timetype.h聽聽聽//聽For聽TimeType聽class<br />聽聽.<br />聽聽.<br />聽聽.<br />enum聽ColorType聽{RED,聽GREEN,聽BLUE};<br />struct聽PatientRec<br />{<br />聽聽聽聽int聽idNum;<br />聽聽聽聽int聽height;<br />聽聽聽聽int聽weight;<br />};<br /><br />int聽聽聽聽聽聽聽聽alpha;<br />ColorType聽聽color;<br />PatientRec聽patient;<br />TimeType聽聽聽startTime(8,聽30,聽0);<br /><br />int*聽聽聽聽聽聽聽聽intPtr聽=聽&alpha;<br />ColorType*聽聽colorPtr聽=聽&color;<br />PatientRec*聽patientPtr聽=聽&patient;<br />TimeType*聽聽聽timePtr聽=聽&startTime;</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 variables </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">intPrt</font><font face="Times New Roman, Times, Serif" size="3">, </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">colorPtr</font><font face="Times New Roman, Times, Serif" size="3">, </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">patientPtr</font><font face="Times New Roman, Times, Serif" size="3">, and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">timePtr</font><font face="Times New Roman, Times, Serif" size="3"> are all pointer variables. </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">intPtr</font><font face="Times New Roman, Times, Serif" size="3"> points to (contains the address of) a variable of type </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">int</font><font face="Times New Roman, Times, Serif" size="3">; </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">colorPtr</font><font face="Times New Roman, Times, Serif" size="3"> points to a variable of type </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Color;</font><font face="Times New Roman, Times, Serif" size="3"></font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">patientPtr</font><font face="Times New Roman, Times, Serif" size="3"> points to a </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">struct</font><font face="Times New Roman, Times, Serif" size="3"> variable of type </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">PatientRec;</font><font face="Times New Roman, Times, Serif" size="3"> and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">timePtr</font><font face="Times New Roman, Times, Serif" size="3"> points to a class object of type </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">TimeType</font><font face="Times New Roman, Times, Serif" 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">The expression </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">*intPtr</font><font face="Times New Roman, Times, Serif" size="3"> denotes the variable pointed to by </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">intPtr</font><font face="Times New Roman, Times, Serif" size="3">. The pointed-to variable can contain any int value. The expression </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">*colorPtr</font><font face="Times New Roman, Times, Serif" size="3"> denotes a variable of type </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">ColorType</font><font face="Times New Roman, Times, Serif" size="3">. It can contain </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">RED</font><font face="Times New Roman, Times, Serif" size="3">, </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">GREEN</font><font face="Times New Roman, Times, Serif" size="3">, or </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">BLUE</font><font face="Times New Roman, Times, Serif" size="3">. The</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_971.html">< previous page</a></td> <td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_972</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_973.html">next page ></a></td> </tr> </table> </body> </html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -