亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? ch08.htm

?? good book for learning c++ standard language
?? HTM
?? 第 1 頁 / 共 5 頁
字號:
</BLOCKQUOTE>

<P><A NAME="Heading40"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 8.9. Creating
a stray pointer.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">
1:     // Listing 8.9
2:     // Demonstrates a stray pointer
3:     typedef unsigned short int USHORT;
4:     #include &lt;iostream.h&gt;
5:
6:     int main()
7:     {
8:        USHORT * pInt = new USHORT;
9:        *pInt = 10;
10:       cout &lt;&lt; &quot;*pInt: &quot; &lt;&lt; *pInt &lt;&lt; endl;
11:       delete pInt;
12:       pInt = 0;
13:       long * pLong = new long;
14:       *pLong = 90000;
15:       cout &lt;&lt; &quot;*pLong: &quot; &lt;&lt; *pLong &lt;&lt; endl;
16:
17:       *pInt = 20;      // uh oh, this was deleted!
18:
19:       cout &lt;&lt; &quot;*pInt: &quot; &lt;&lt; *pInt  &lt;&lt; endl;
20:       cout &lt;&lt; &quot;*pLong: &quot; &lt;&lt; *pLong  &lt;&lt; endl;
21:       delete pLong;
22:    return 0;
<TT>23: }</TT></FONT>
<FONT COLOR="#0066FF">
Output: *pInt:   10
*pLong:  90000
*pInt:   20
*pLong:  65556
Null pointer assignment
</FONT></PRE>
<P>(Your output may look different.)</P>
<P><FONT COLOR="#000077"><B>Analysis: </B></FONT>Line 8 declares <TT>pInt</TT> to
be a pointer to <TT>USHORT</TT>, and <TT>pInt</TT> is pointed to newly allocated
memory. Line 9 puts the value <TT>10</TT> in that memory, and line 10 prints its
value. After the value is printed, <TT>delete</TT> is called on the pointer. <TT>pInt</TT>
is now a stray, or dangling, pointer.<BR>
Line 13 declares a new pointer, <TT>pLong</TT>, which is pointed at the memory allocated
by <TT>new</TT>. <BR>
<BR>
Line 14 assigns the value <TT>90000</TT> to <TT>pLong</TT>, and line 15 prints its
value.</P>
<P>Line 17 assigns the value <TT>20</TT> to the memory that <TT>pInt</TT> points
to, but <TT>pInt</TT> no longer points anywhere that is valid. The memory that <TT>pInt</TT>
points to was freed by the call to <TT>delete</TT>, so assigning a value to that
memory is certain disaster.</P>
<P>Line 19 prints the value at <TT>pInt</TT>. Sure enough, it is <TT>20</TT>. Line
20 prints <TT>20</TT>, the value at <TT>pLong</TT>; it has suddenly been changed
to <TT>65556</TT>. Two questions arise:<BR>
<BR>
<B>1.</B> How could <TT>pLong</TT>'s value change, given that <TT>pLong</TT> wasn't
touched?<BR>
<BR>
<B>2</B>. Where did the 20 go when <TT>pInt</TT> was used in line 17?<BR>
<BR>
As you might guess, these are related questions. When a value was placed at <TT>pInt</TT>
in line 17, the compiler happily placed the value <TT>20</TT> at the memory location
that <TT>pInt</TT> previously pointed to. However, because that memory was freed
in line 11, the compiler was free to reassign it. When <TT>pLong</TT> was created
in line 13, it was given <TT>pInt</TT>'s old memory location. (On some computers
this may not happen, depending on where in memory these values are stored.) When
the value <TT>20</TT> was assigned to the location that <TT>pInt</TT> previously
pointed to, it wrote over the value pointed to by <TT>pLong</TT>. This is called
&quot;stomping on a pointer.&quot; It is often the unfortunate outcome of using a
stray pointer.<BR>
This is a particularly nasty bug, because the value that changed wasn't associated
with the stray pointer. The change to the value at <TT>pLong</TT> was a side effect
of the misuse of <TT>pInt</TT>. In a large program, this would be very difficult
to track down. <BR>
<BR>
Just for fun, here are the details of how 65,556 got into that memory address:

<DL>
	<DD><B>1.</B> <TT>pInt</TT> was pointed at a particular memory location, and the
	value <TT>10</TT> was assigned.<BR>
	<BR>
	<B>2.</B> <TT>delete</TT> was called on <TT>pInt</TT>, which told the compiler that
	it could put something else at that location. Then <TT>pLong</TT> was assigned the
	same memory location.<BR>
	<BR>
	<B>3.</B> The value <TT>90000</TT> was assigned to <TT>*pLong</TT>. The particular
	computer used in this example stored the four-byte value of 90,000 (00 01 5F 90)
	in byte-swapped order. Therefore, it was stored as 5F 90 00 01.<BR>
	<BR>
	<B>4.</B> <TT>pInt</TT> was assigned the value <TT>20</TT>--or 00 14 in hexadecimal
	notation. Because <TT>pInt</TT> still pointed to the same address, the first two
	bytes of <TT>pLong</TT> were overwritten, leaving 00 14 00 01.<BR>
	<BR>
	<B>5.</B> The value at <TT>pLong</TT> was printed, reversing the bytes back to their
	correct order of 00 01 00 14, which was translated into the DOS value of <TT>65556</TT>.<BR>
	<BR>
	
<HR>
<B>DO</B> use <TT>new</TT> to create objects on the free store.<B> DO</B> use <TT>delete</TT>
	to destroy objects on the free store and to return their memory. <B>DON'T</B> forget
	to balance all <TT>new</TT> statements with a <TT>delete</TT> statement. <B>DON'T</B>
	forget to assign <TT>null</TT> (<TT>0</TT>) to all pointers that you call <TT>delete</TT>
	on. <B>DO</B> check the value returned by <TT>new</TT>. 
<HR>

</DL>

<H3 ALIGN="CENTER"><A NAME="Heading42"></A><FONT COLOR="#000077">const Pointers</FONT></H3>
<P>You can use the keyword <TT>const</TT> for pointers before the type, after the
type, or in both places. For example, all of the following are legal declarations:</P>
<PRE><FONT COLOR="#0066FF">
const int * pOne;
int * const pTwo;
const int * const pThree;
</FONT></PRE>
<P><TT>pOne</TT> is a pointer to a constant integer. The value that is pointed to
can't be changed.</P>
<P><TT>pTwo</TT> is a constant pointer to an integer. The integer can be changed,
but <TT>pTwo</TT> can't point to anything else.</P>
<P><TT>pThree</TT> is a constant pointer to a constant integer. The value that is
pointed to can't be changed, and <TT>pThree</TT> can't be changed to point to anything
else.</P>
<P>The trick to keeping this straight is to look to the right of the keyword <TT>const</TT>
to find out what is being declared constant. If the type is to the right of the keyword,
it is the value that is constant. If the variable is to the right of the keyword
<TT>const</TT>, it is the pointer variable itself that is constant.</P>
<PRE><FONT COLOR="#0066FF">const int * p1;  // the int pointed to is constant
int * const p2;  // p2 is constant, it can't point to anything else
</FONT></PRE>
<H4 ALIGN="CENTER"><A NAME="Heading43"></A><FONT COLOR="#000077">const Pointers and
const Member Functions</FONT></H4>
<P>On Day 6, &quot;Basic Classes,&quot; you learned that you can apply the keyword
<TT>const</TT> to a member function. When a function is declared <TT>const</TT>,
the compiler flags as an error any attempt to change data in the object from within
that function.</P>
<P>If you declare a pointer to a <TT>const</TT> object, the only methods that you
can call with that pointer are <TT>const</TT> methods. Listing 8.10 illustrates this.</P>
<P><A NAME="Heading44"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 8.10. Using
pointers to const objects.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">
1:      // Listing 8.10
2:      // Using pointers with const methods
3:
4:      #include &lt;iostream.h&gt;
5:
6:      class Rectangle
7:      {
8:      public:
9:           Rectangle();
10:           ~Rectangle();
11:           void SetLength(int length) { itsLength = length; }
12:           int GetLength() const { return itsLength; }
13:
14:           void SetWidth(int width) { itsWidth = width; }
15:           int GetWidth() const { return itsWidth; }
16:
17:      private:
18:           int itsLength;
19:           int itsWidth;
20:      };
21:
22:      Rectangle::Rectangle():
23:      itsWidth(5),
24:      itsLength(10)
25:      {}
26:
27:      Rectangle::~Rectangle()
28:      {}
29:
30:      int main()
31:      {
32:           Rectangle* pRect =  new Rectangle;
33:           const Rectangle * pConstRect = new Rectangle;
34:           Rectangle * const pConstPtr = new Rectangle;
35:
36:           cout &lt;&lt; &quot;pRect width: &quot; &lt;&lt; pRect-&gt;GetWidth() &lt;&lt; &quot; feet\n&quot;;
37:           cout &lt;&lt; &quot;pConstRect width: &quot; &lt;&lt; pConstRect-&gt;GetWidth() &lt;&lt; &quot; feet\n&quot;;
38:           cout &lt;&lt; &quot;pConstPtr width: &quot; &lt;&lt; pConstPtr-&gt;GetWidth() &lt;&lt; &quot; feet\n&quot;;
39:
40:           pRect-&gt;SetWidth(10);
41:           // pConstRect-&gt;SetWidth(10);
42:           pConstPtr-&gt;SetWidth(10);
43:
44:           cout &lt;&lt; &quot;pRect width: &quot; &lt;&lt; pRect-&gt;GetWidth() &lt;&lt; &quot; feet\n&quot;;
45:           cout &lt;&lt; &quot;pConstRect width: &quot; &lt;&lt; pConstRect-&gt;GetWidth() &lt;&lt; &quot; feet\n&quot;;
46:           cout &lt;&lt; &quot;pConstPtr width: &quot; &lt;&lt; pConstPtr-&gt;GetWidth() &lt;&lt; &quot; feet\n&quot;;
47:      return 0;
<TT>48: }</TT></FONT>
<FONT COLOR="#0066FF">
Output: pRect width: 5 feet
pConstRect width: 5 feet
pConstPtr width: 5 feet
pRect width: 10 feet
pConstRect width: 5 feet
pConstPtr width: 10 feet
</FONT></PRE>
<P><FONT COLOR="#000077"><B>Analysis: </B></FONT>Lines 6-20 declare <TT>Rectangle</TT>.
Line 15 declares the <TT>GetWidth()</TT> member method <TT>const</TT>. Line 32 declares
a pointer to <TT>Rectangle</TT>. Line 33 declares <TT>pConstRect</TT>, which is a
pointer to a constant <TT>Rectangle</TT>. Line 34 declares <TT>pConstPtr</TT>, which
is a constant pointer to <TT>Rectangle</TT>.<BR>
Lines 36-38 print their values.</P>
<P>In line 40, <TT>pRect</TT> is used to set the width of the rectangle to <TT>10</TT>.
In line 41, <TT>pConstRect</TT> would be used, but it was declared to point to a
constant <TT>Rectangle</TT>. Therefore, it cannot legally call a non-<TT>const</TT>
member function; it is commented out. In line 38, <TT>pConstPtr</TT> calls <TT>SetAge()</TT>.
<TT>pConstPtr</TT> is declared to be a constant pointer to a rectangle. In other
words, the pointer is constant and cannot point to anything else, but the rectangle
is not constant.
<H4 ALIGN="CENTER"><A NAME="Heading46"></A><FONT COLOR="#000077">const this Pointers</FONT></H4>
<P>When you declare an object to be <TT>const</TT>, you are in effect declaring that
the <TT>this</TT> pointer is a pointer to a <TT>const</TT> object. A <TT>const</TT>
<TT>this</TT> pointer can be used only with <TT>const</TT> mem- ber functions.</P>
<P>Constant objects and constant pointers will be discussed again tomorrow, when
references to constant objects are discussed.


<BLOCKQUOTE>
	<P>
<HR>
<B>DO</B> protect objects passed by reference with <TT>const</TT> if they should
	not be changed. <B>DO </B>pass by reference when the object can be changed.<B> DO</B>
	pass by value when small objects should not be changed. 
<HR>


</BLOCKQUOTE>

<H3 ALIGN="CENTER"><A NAME="Heading47"></A><FONT COLOR="#000077">Summary</FONT></H3>
<P>Pointers provide a powerful way to access data by indirection. Every variable
has an address, which can be obtained using the <TT>address of</TT> operator (<TT>&amp;</TT>).
The address can be stored in a pointer.</P>
<P>Pointers are declared by writing the type of object that they point to, followed
by the indirection operator (<TT>*</TT>) and the name of the pointer. Pointers should
be initialized to point to an object or to <TT>null</TT> (<TT>0</TT>).</P>
<P>You access the value at the address stored in a pointer by using the indirection
operator (<TT>*</TT>). You can declare <TT>const</TT> pointers, which can't be reassigned
to point to other objects, and pointers to <TT>const</TT> objects, which can't be
used to change the objects to which they point.</P>
<P>To create new objects on the free store, you use the <TT>new</TT> keyword and
assign the address that is returned to a pointer. You free that memory by calling
the <TT>delete</TT> keyword on the pointer. <TT>delete</TT> frees the memory, but
it doesn't destroy the pointer. Therefore, you must reassign the pointer after its
memory has been freed.
<H3 ALIGN="CENTER"><A NAME="Heading48"></A><FONT COLOR="#000077">Q&amp;A</FONT></H3>

<DL>
	<DD><B>Q. Why are pointers so important?<BR>
	</B><BR>
	<B>A.</B> Today you saw how pointers are used to hold the address of objects on the
	free store and how they are used to pass arguments by reference. In addition, on
	Day 13, &quot;Polymorphism,&quot; you'll see how pointers are used in class polymorphism.<BR>
	<BR>
	<B>Q. Why should I bother to declare anything on the free store?<BR>
	</B><BR>
	<B>A.</B> Objects on the free store persist after the return of a function. Additionally,
	the ability to store objects on the free store enables you to decide at runtime how
	many objects you need, instead of having to declare this in advance. This is explored
	in greater depth tomorrow.<BR>
	<BR>
	<B>Q. Why should I declare an object const if it limits what I can do with it?<BR>
	</B><BR>

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
亚洲综合色区另类av| 亚洲天堂网中文字| 91精品婷婷国产综合久久性色| 91婷婷韩国欧美一区二区| 国产不卡在线视频| 粉嫩高潮美女一区二区三区| 黄色资源网久久资源365| 久久不见久久见免费视频1| 日本人妖一区二区| 国产在线麻豆精品观看| 国产v综合v亚洲欧| 91色porny| 在线不卡欧美精品一区二区三区| 欧美日韩一区二区三区在线看| 欧美性色aⅴ视频一区日韩精品| 精品视频在线看| 日韩欧美高清在线| 久久视频一区二区| 国产精品久久网站| 亚洲国产精品综合小说图片区| 丝袜诱惑亚洲看片| 国产高清亚洲一区| 色婷婷精品久久二区二区蜜臂av| 欧美日韩久久一区二区| 日韩欧美国产综合在线一区二区三区| 久久综合视频网| 亚洲欧洲精品天堂一级| 日韩一区欧美二区| 成人91在线观看| 欧美日韩免费在线视频| 精品卡一卡二卡三卡四在线| 专区另类欧美日韩| 久久爱www久久做| 色婷婷av一区二区三区之一色屋| 7777精品伊人久久久大香线蕉超级流畅 | 久久精品国产99久久6| 懂色av一区二区三区蜜臀| 精品视频在线免费看| 精品国产成人系列| 亚洲免费观看高清完整版在线观看 | 欧美肥妇free| 国产精品久久久久久久久搜平片| 亚欧色一区w666天堂| 成人午夜碰碰视频| 日韩一区二区三区在线视频| 亚洲欧美在线高清| 久久精品99久久久| 在线视频一区二区三区| 久久欧美中文字幕| 亚洲成人综合在线| 91在线观看成人| 国产香蕉久久精品综合网| 亚洲国产成人av| 99re成人在线| 国产日韩欧美精品综合| 蜜臀av性久久久久蜜臀av麻豆| 91在线观看美女| 国产精品久久久久一区二区三区| 久久超碰97中文字幕| 欧美精品丝袜中出| 一级特黄大欧美久久久| 99re成人在线| 亚洲视频每日更新| 9色porny自拍视频一区二区| 久久久国产精品午夜一区ai换脸| 日本sm残虐另类| 在线综合亚洲欧美在线视频| 亚洲一二三四在线| 91极品美女在线| 一区二区三区在线视频观看58| 97久久超碰国产精品| 国产精品美女久久久久久久久久久| 国产在线视频精品一区| 久久老女人爱爱| 成人影视亚洲图片在线| 亚洲国产高清aⅴ视频| 成人国产精品免费观看视频| 国产精品久久久久久久久久久免费看| 国产91在线|亚洲| 国产精品美日韩| 91日韩一区二区三区| 亚洲综合一二区| 欧美日韩国产bt| 久久黄色级2电影| 国产三级精品三级| www.66久久| 亚洲午夜一区二区| 91精品国产一区二区三区香蕉 | 国产成人免费视频网站高清观看视频| 精品入口麻豆88视频| 国产乱理伦片在线观看夜一区 | 一区二区三区四区视频精品免费| 94-欧美-setu| 日韩av不卡一区二区| 精品成人一区二区| 波多野结衣精品在线| 亚洲一区二区三区不卡国产欧美| 欧美美女网站色| 国产一区二区免费看| 亚洲欧美综合色| 欧美一二三四区在线| 成人精品视频一区二区三区| 1区2区3区精品视频| 欧美肥大bbwbbw高潮| 国产大陆a不卡| 亚欧色一区w666天堂| 日本一区二区三区四区| 欧美日韩日本视频| 国产mv日韩mv欧美| 日日摸夜夜添夜夜添精品视频 | 蜜桃91丨九色丨蝌蚪91桃色| 国产精品欧美一级免费| 欧美精品免费视频| 成人avav影音| 精品一区免费av| 亚洲美女一区二区三区| 精品国产乱码久久久久久久| 一本大道av伊人久久综合| 日本美女一区二区| 亚洲欧美日韩久久| 久久久亚洲精品一区二区三区| 色婷婷激情综合| 丁香婷婷深情五月亚洲| 老司机午夜精品| 亚洲成a人片综合在线| 欧美极品少妇xxxxⅹ高跟鞋 | 国产视频一区二区在线| 91精品国产综合久久精品app| 成人av动漫在线| 国产成人日日夜夜| 久久99精品国产.久久久久| 亚洲国产精品麻豆| 亚洲欧洲av在线| 中文子幕无线码一区tr| 日韩免费观看高清完整版 | 中文字幕在线一区二区三区| 91精品国产高清一区二区三区蜜臀| 99久久精品99国产精品| 国产精品主播直播| 久久成人羞羞网站| 另类小说图片综合网| 日本欧美大码aⅴ在线播放| 亚洲va在线va天堂| 亚洲第一久久影院| 天天做天天摸天天爽国产一区| 亚洲人成网站精品片在线观看| 日本一区二区三级电影在线观看 | 色婷婷国产精品久久包臀 | 日韩一区二区三| 欧美日韩mp4| 欧美久久久久免费| 欧美日韩一区久久| 67194成人在线观看| 欧美一区二区三区的| 日韩欧美色综合网站| 精品国产一二三区| 久久亚洲免费视频| 欧美精品一区二区三区久久久| 久久综合av免费| 久久精品一区二区三区av| 中文字幕精品一区二区精品绿巨人| 国产亚洲短视频| 亚洲欧美国产毛片在线| 亚洲一二三四久久| 日韩制服丝袜av| 国产原创一区二区三区| 成人免费高清在线| 色www精品视频在线观看| 欧美四级电影在线观看| 日韩视频免费观看高清完整版在线观看| 欧美成人性战久久| 国产精品欧美综合在线| 午夜精品一区二区三区电影天堂 | 欧美—级在线免费片| 中文字幕中文在线不卡住| 亚洲综合久久久久| 免费看欧美美女黄的网站| 国产精品自产自拍| 日本久久一区二区三区| 9191久久久久久久久久久| 久久嫩草精品久久久精品| 国产精品美女久久久久aⅴ国产馆| 亚洲欧美日韩国产综合在线| 蜜臀久久99精品久久久久宅男| 成人午夜激情在线| 欧美老女人第四色| 国产精品毛片高清在线完整版| 亚洲国产日韩精品| 国产麻豆精品一区二区| 日本韩国精品在线| 久久天堂av综合合色蜜桃网| 亚洲激情中文1区| 久久狠狠亚洲综合| 91国偷自产一区二区开放时间 | 在线观看免费一区| 久久伊人蜜桃av一区二区| 亚洲国产视频一区| 成人伦理片在线| 日韩你懂的电影在线观看| 亚洲久草在线视频|