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

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

?? ch08.htm

?? good book for learning c++ standard language
?? HTM
?? 第 1 頁 / 共 5 頁
字號:
23               cout << "Destructor called.\n";
24        }
25
26        int main()
27        {
28               cout << "SimpleCat Frisky...\n";
29               SimpleCat Frisky;
30               cout << "SimpleCat *pRags = new SimpleCat...\n";
31               SimpleCat * pRags = new SimpleCat;
32               cout << "delete pRags...\n";
33               delete pRags;
34               cout << "Exiting, watch Frisky go...\n";
35        return 0;
<TT>36 }</TT></FONT>
<FONT COLOR="#0066FF">
Output: SimpleCat Frisky...
Constructor called.
SimpleCat *pRags = new SimpleCat..
Constructor called.
delete pRags...
Destructor called.
Exiting, watch Frisky go...
Destructor called.
</FONT></PRE>
<P><FONT COLOR="#000077"><B>Analysis:</B></FONT><B> </B>Lines 6-13 declare the stripped-down
class <TT>SimpleCat</TT>. Line 9 declares <TT>SimpleCat</TT>'s constructor, and lines
15-19 contain its definition. Line 10 declares <TT>SimpleCat</TT>'s destructor, and
lines 21-24 contain its definition.<BR>
In line 29, <TT>Frisky</TT> is created on the stack, which causes the constructor
to be called. In line 31, the <TT>SimpleCat</TT> pointed to by <TT>pRags</TT> is
created on the heap; the constructor is called again. In line 33, delete is called
on <TT>pRags</TT>, and the destructor is called. When the function ends, <TT>Frisky</TT>
goes out of scope, and the destructor is called.
<H3 ALIGN="CENTER"><A NAME="Heading30"></A><FONT COLOR="#000077">Accessing Data Members</FONT></H3>
<P>You accessed data members and functions by using the dot (<TT>.</TT>) operator
for <TT>Cat</TT> objects created locally. To access the <TT>Cat</TT> object on the
free store, you must dereference the pointer and call the dot operator on the object
pointed to by the pointer. Therefore, to access the <TT>GetAge</TT> member function,
you would write</P>
<PRE><FONT COLOR="#0066FF"> (*pRags).GetAge();
</FONT></PRE>
<P>Parentheses are used to assure that <TT>pRags</TT> is dereferenced before <TT>GetAge()</TT>
is accessed.</P>
<P>Because this is cumbersome, C++ provides a shorthand operator for indirect access:
the <TT>points-to</TT> operator (<TT>-&gt;</TT>), which is created by typing the
dash (<TT>-</TT>) immediately followed by the greater-than symbol (<TT>&gt;</TT>).
C++ treats this as a single symbol. Listing 8.6 demonstrates accessing member variables
and functions of objects created on the free store.</P>
<P><A NAME="Heading31"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 8.6. Accessing
member data of objects on the free store.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">
1:     // Listing 8.6
2:     // Accessing data members of objects on the heap
3:
4:      #include &lt;iostream.h&gt;
5:
6:      class SimpleCat
7:      {
8:      public:
9:             SimpleCat() {itsAge = 2; }
10:             ~SimpleCat() {}
11:             int GetAge() const { return itsAge; }
12:             void SetAge(int age) { itsAge = age; }
13:     private:
14:             int itsAge;
15:        };
16:
17:        int main()
18:        {
19:               SimpleCat * Frisky = new SimpleCat;
20:               cout &lt;&lt; &quot;Frisky is &quot; &lt;&lt; Frisky-&gt;GetAge() &lt;&lt; &quot; years old\n&quot;;
21:               Frisky-&gt;SetAge(5);
22:               cout &lt;&lt; &quot;Frisky is &quot; &lt;&lt; Frisky-&gt;GetAge() &lt;&lt; &quot; years old\n&quot;;
23:               delete Frisky;
24:        return 0;
<TT>25: }</TT></FONT>
<FONT COLOR="#0066FF">
Output: Frisky is 2 years old
Frisky is 5 years old
</FONT></PRE>
<P><FONT COLOR="#000077"><B>Analysis:</B></FONT><B> </B>In line 19, a <TT>SimpleCat</TT>
object is instantiated on the free store. The default constructor sets its age to
<TT>2</TT>, and the <TT>GetAge()</TT> method is called in line 20. Because this is
a pointer, the indirection operator (<TT>-&gt;</TT>) is used to access the member
data and functions. In line 21, the <TT>SetAge()</TT> method is called, and <TT>GetAge()</TT>
is accessed again in line 22.
<H3 ALIGN="CENTER"><A NAME="Heading33"></A><FONT COLOR="#000077">Member Data on the
Free Store</FONT></H3>
<P>One or more of the data members of a class can be a pointer to an object on the
free store. The memory can be allocated in the class constructor or in one of its
methods, and it can be deleted in its destructor, as Listing 8.7 illustrates.</P>
<P><A NAME="Heading34"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 8.7. Pointers
as member data.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">1:  // Listing 8.7
2:  // Pointers as data members
3:
4:    #include &lt;iostream.h&gt;
5:
6:    class SimpleCat
7:    {
8:    public:
9:            SimpleCat();
10:            ~SimpleCat();
11:            int GetAge() const { return *itsAge; }
12:            void SetAge(int age) { *itsAge = age; }
13:
14:            int GetWeight() const { return *itsWeight; }
15:            void setWeight (int weight) { *itsWeight = weight; }
16:
17:    private:
18:            int * itsAge;
19:            int * itsWeight;
20:       };
21:
22:       SimpleCat::SimpleCat()
23:       {
24:            itsAge = new int(2);
25:            itsWeight = new int(5);
26:       }
27:
28:       SimpleCat::~SimpleCat()
29:       {
30:            delete itsAge;
31:            delete itsWeight;
32:       }
33:
34:       int main()
35:       {
36:              SimpleCat *Frisky = new SimpleCat;
37:              cout &lt;&lt; &quot;Frisky is &quot; &lt;&lt; Frisky-&gt;GetAge() &lt;&lt; &quot; years old\n&quot;;
38:              Frisky-&gt;SetAge(5);
39:              cout &lt;&lt; &quot;Frisky is &quot; &lt;&lt; Frisky-&gt;GetAge() &lt;&lt; &quot; years old\n&quot;;
40:              delete Frisky;
41:       return 0;
<TT>42: }</TT></FONT>
<FONT COLOR="#0066FF">
Output: Frisky is 2 years old
Frisky is 5 years old
</FONT></PRE>
<P><FONT COLOR="#000077"><B>Analysis:</B></FONT><B> </B>The class <TT>SimpleCat</TT>
is declared to have two member variables--both of which are pointers to integers--on
lines 14 and 15. The constructor (lines 22-26) initializes the pointers to memory
on the free store and to the default values.<BR>
The destructor (lines 28-32) cleans up the allocated memory. Because this is the
destructor, there is no point in assigning these pointers to <TT>null</TT>, as they
will no longer be accessible. This is one of the safe places to break the rule that
deleted pointers should be assigned to <TT>null</TT>, although following the rule
doesn't hurt.</P>
<P>The calling function (in this case, <TT>main()</TT>) is unaware that <TT>itsAge</TT>
and <TT>itsWeight</TT> are point-ers to memory on the free store. <TT>main()</TT>
continues to call <TT>GetAge()</TT> and <TT>SetAge()</TT>, and the details of the
memory management are hidden in the implementation of the class--as they should be.</P>
<P>When <TT>Frisky</TT> is deleted in line 40, its destructor is called. The destructor
deletes each of its member pointers. If these, in turn, point to objects of other
user-defined classes, their destructors are called as well.
<H3 ALIGN="CENTER"><A NAME="Heading36"></A><FONT COLOR="#000077">The this Pointer</FONT></H3>
<P>Every class member function has a hidden parameter: the <TT>this</TT> pointer.
<TT>this</TT> points to the individual object. Therefore, in each call to <TT>GetAge()</TT>
or <TT>SetAge()</TT>, the <TT>this</TT> pointer for the object is included as a hidden
parameter.</P>
<P>It is possible to use the <TT>this</TT> pointer explicitly, as Listing 8.8 illustrates.</P>
<P><A NAME="Heading37"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 8.8. Using the
this pointer.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">
1:      // Listing 8.8
2:      // Using the this pointer
3:
4:      #include &lt;iostream.h&gt;
5:
6:      class Rectangle
7:      {
8:      public:
9:           Rectangle();
10:           ~Rectangle();
11:           void SetLength(int length) { this-&gt;itsLength = length; }
12:           int GetLength() const { return this-&gt;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:      {
24:          itsWidth = 5;
25:          itsLength = 10;
26:      }
27:      Rectangle::~Rectangle()
28:      {}
29:
30:      int main()
31:      {
32:           Rectangle theRect;
33:           cout &lt;&lt; &quot;theRect is &quot; &lt;&lt; theRect.GetLength() &lt;&lt; &quot; feet long.\n&quot;;
34:           cout &lt;&lt; &quot;theRect is &quot; &lt;&lt; theRect.GetWidth() &lt;&lt; &quot; feet wide.\n&quot;;
35:           theRect.SetLength(20);
36:           theRect.SetWidth(10);
37:           cout &lt;&lt; &quot;theRect is &quot; &lt;&lt; theRect.GetLength()&lt;&lt; &quot; feet long.\n&quot;;
38:           cout &lt;&lt; &quot;theRect is &quot; &lt;&lt; theRect.GetWidth()&lt;&lt; &quot; feet wide.\n&quot;;
39:      return 0;
<TT>40: }</TT></FONT>
<FONT COLOR="#0066FF">
Output: theRect is 10 feet long.
theRect is 5 feet long.
theRect is 20 feet long.
theRect is 10 feet long.
</FONT></PRE>
<P><FONT COLOR="#000077"><B>Analysis: </B></FONT>The <TT>SetLength()</TT> and <TT>GetLength()</TT>
accessor functions explicitly use the <TT>this</TT> pointer to access the member
variables of the <TT>Rectangle</TT> object. The <TT>SetWidth</TT> and <TT>GetWidth</TT>
accessors do not. There is no difference in their behavior, although the syntax is
easier to understand.<BR>
If that were all there was to the <TT>this</TT> pointer, there would be little point
in bothering you with it. The <TT>this</TT> pointer, however, is a pointer; it stores
the memory address of an object. As such, it can be a powerful tool.</P>
<P>You'll see a practical use for the <TT>this</TT> pointer on Day 10, &quot;Advanced
Functions,&quot; when operator overloading is discussed. For now, your goal is to
know about the <TT>this</TT> pointer and to understand what it is: a pointer to the
object itself.</P>
<P>You don't have to worry about creating or deleting the <TT>this</TT> pointer.
The compiler takes care of that.
<H3 ALIGN="CENTER"><A NAME="Heading39"></A><FONT COLOR="#000077">Stray or Dangling
Pointers</FONT></H3>
<P>One source of bugs that are nasty and difficult to find is stray pointers. A stray
pointer is created when you call <TT>delete</TT> on a pointer--thereby freeing the
memory that it points to--and later try to use that pointer again without reassigning
it.</P>
<P>It is as though the Acme Mail Order company moved away, and you still pressed
the programmed button on your phone. It is possible that nothing terrible happens--a
telephone rings in a deserted warehouse. Perhaps the telephone number has been reassigned
to a munitions factory, and your call detonates an explosive and blows up your whole
city!</P>
<P>In short, be careful not to use a pointer after you have called <TT>delete</TT>
on it. The pointer still points to the old area of memory, but the compiler is free
to put other data there; using the pointer can cause your program to crash. Worse,
your program might proceed merrily on its way and crash several minutes later. This
is called a time bomb, and it is no fun. To be safe, after you delete a pointer,
set it to <TT>null</TT> (<TT>0</TT>). This disarms the pointer.


<BLOCKQUOTE>
	<P>
<HR>
<FONT COLOR="#000077"><B>NOTE:</B></FONT><B> </B>Stray pointers are often called
	wild pointers or dangling pointers. 
<HR>


</BLOCKQUOTE>

<P>Listing 8.9 illustrates creating a stray pointer.


<BLOCKQUOTE>
	<P>
<HR>
<FONT COLOR="#000077"><B>WARNING:</B></FONT><B> </B>This program intentionally creates
	a stray pointer. Do NOT run this program--it will crash, if you are lucky. 
<HR>


?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
久久一区二区视频| 国产综合一区二区| 免费久久精品视频| eeuss鲁片一区二区三区| 在线成人小视频| 国产精品久久久久久户外露出 | 久久久久国色av免费看影院| 中文字幕一区二区三区四区不卡 | 国模大尺度一区二区三区| 91免费看视频| 国产女人水真多18毛片18精品视频| 午夜久久久久久| 91黄色在线观看| 国产欧美一区二区三区鸳鸯浴| 亚洲超碰97人人做人人爱| 成人国产精品免费网站| 337p粉嫩大胆噜噜噜噜噜91av| 三级不卡在线观看| 欧美视频中文字幕| 亚洲一区免费视频| 91丝袜呻吟高潮美腿白嫩在线观看| 久久精品人人爽人人爽| 日本成人在线不卡视频| 欧美精品在线一区二区| 亚洲国产va精品久久久不卡综合| 9人人澡人人爽人人精品| 国产精品拍天天在线| 国产成人自拍在线| 国产亚洲精品bt天堂精选| 久久精品久久99精品久久| 国产精品高潮呻吟| 处破女av一区二区| 久久久国产一区二区三区四区小说 | 丝袜美腿亚洲色图| 欧美喷潮久久久xxxxx| 午夜电影久久久| 91精品在线免费| 男人的j进女人的j一区| 欧美一区二区久久| 久国产精品韩国三级视频| 精品99一区二区| 成人国产亚洲欧美成人综合网| 国产精品久久久久aaaa樱花| 91视频在线观看免费| 亚洲国产综合91精品麻豆| 91精品国产一区二区| 蜜臀av国产精品久久久久| 精品久久久久久久久久久久包黑料| 精品亚洲成a人| 中文字幕亚洲在| 欧美色视频在线| 美国一区二区三区在线播放| 精品久久99ma| 99视频一区二区三区| 亚洲影视在线播放| 日韩免费高清av| 波多野结衣在线一区| 亚洲精品高清视频在线观看| 欧美日韩久久一区二区| 精品午夜一区二区三区在线观看| 国产女主播在线一区二区| 91高清在线观看| 久久99久久99精品免视看婷婷| 欧美—级在线免费片| 在线观看视频欧美| 国产麻豆视频一区| 洋洋av久久久久久久一区| 精品国产一区二区三区久久久蜜月 | 亚洲欧美偷拍卡通变态| 3d动漫精品啪啪| 成人精品视频一区二区三区| 亚洲国产欧美另类丝袜| 久久精品视频一区二区三区| 色先锋资源久久综合| 九九九久久久精品| 一区二区三区精品| 国产午夜精品一区二区三区四区| 在线观看日韩毛片| 丰满放荡岳乱妇91ww| 日韩电影免费在线看| 亚洲男同性恋视频| 久久久久久久综合| 制服丝袜亚洲网站| 一本到不卡精品视频在线观看| 国产精品一区免费视频| 天堂va蜜桃一区二区三区漫画版 | 一本到一区二区三区| 国产剧情一区二区| 日本va欧美va精品| 亚洲精品第一国产综合野| 国产免费成人在线视频| 亚洲欧美在线另类| 精品国产一区a| 欧美日韩午夜在线| 色8久久精品久久久久久蜜| 国产成人精品亚洲日本在线桃色| 免费久久精品视频| 亚洲成av人片观看| 亚洲精品免费一二三区| 亚洲国产精品精华液ab| 精品对白一区国产伦| 91精品国产综合久久精品app | 亚洲国产精品天堂| 亚洲欧美精品午睡沙发| 中文一区二区完整视频在线观看| 精品国产91乱码一区二区三区| 欧美精品一卡二卡| 欧美肥妇毛茸茸| 色成年激情久久综合| 91在线观看高清| 91一区二区三区在线播放| 成人综合激情网| 国产超碰在线一区| 成人免费视频视频在线观看免费| 国产精品一区二区免费不卡| 国产盗摄一区二区三区| 国产精品一级黄| 国产白丝网站精品污在线入口| 韩国精品久久久| 国产成人综合在线| 不卡av在线网| 色婷婷精品大在线视频| 欧美三级中文字| 欧美在线你懂得| 91精品国产综合久久精品| 91精品国模一区二区三区| 日韩欧美你懂的| 久久亚洲一区二区三区四区| 国产无一区二区| 日韩理论在线观看| 亚洲一区二区三区激情| 午夜精品爽啪视频| 国产综合色在线| 成人av资源站| 欧美日韩国产精选| 日韩你懂的在线播放| 国产片一区二区| 亚洲美女屁股眼交| 偷拍一区二区三区| 激情图片小说一区| gogo大胆日本视频一区| 欧美亚洲综合在线| 欧美va日韩va| 亚洲人午夜精品天堂一二香蕉| 亚洲在线一区二区三区| 奇米影视一区二区三区小说| 国产不卡视频一区| 欧美色中文字幕| 久久综合色综合88| 亚洲黄色小说网站| 国产在线不卡一区| 欧美亚洲国产一区二区三区| 欧美sm极限捆绑bd| 艳妇臀荡乳欲伦亚洲一区| 免费视频一区二区| 91浏览器入口在线观看| 亚洲精品一区二区精华| 一区二区三区四区在线播放| 九九九精品视频| 欧美性淫爽ww久久久久无| 久久网站最新地址| 午夜a成v人精品| 99久久精品情趣| 欧美videos中文字幕| 亚洲激情校园春色| 国产成人亚洲综合a∨婷婷图片| 欧美日韩一区二区不卡| 国产精品午夜春色av| 久久精品99国产精品日本| 91国偷自产一区二区开放时间| 久久久精品免费网站| 日韩av电影免费观看高清完整版在线观看| 丁香亚洲综合激情啪啪综合| 欧美一级日韩免费不卡| 亚洲精品国产一区二区精华液 | 免播放器亚洲一区| 欧美色综合天天久久综合精品| 欧美激情一区不卡| 精品一区二区三区在线观看 | 久久久久国产免费免费| 日本欧美一区二区| 欧美三级电影网站| 亚洲精品视频免费观看| 99久久综合国产精品| 久久精品一区二区| 国产真实精品久久二三区| 日韩一二三区不卡| 婷婷开心久久网| 欧美日韩一区二区三区视频| 一区二区三区四区亚洲| 91视频在线看| 亚洲精品日日夜夜| 色呦呦国产精品| 亚洲精品中文字幕乱码三区| 97久久精品人人做人人爽 | 亚洲欧美日韩国产一区二区三区| 国产成人在线色| 久久久国产午夜精品| 国产一区二区女| 国产婷婷色一区二区三区在线|