• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 183,  comments - 10,  trackbacks - 0

            關于含有成員指針的類的復制控制

            一個類中如果含有了指針成員,在不加任何措施的時候,復制類對象是做的位復制操作,即是所謂的淺拷貝,兩個對象的指針式一樣的值,指向同一塊內存區。
            這種情況下,當一個對象刪除其成員指針指向的內存區后,另一個對象的指針成員指向的內存區也就被釋放了。
            如果第一個對象析構時刪除了這個內存區,那么在第二對象析構時造成同一塊內存多次被釋放,程序崩潰。

            解決這個問題有常規上有兩種方法
            一是,進行深拷貝,在拷貝構造函數和復制運算符中進行相應的內存分配和復制工作。析構的時候只是各自析構各自的。
            二是,采用引用計數手段,在拷貝構造函數和復制運算符中對引用計數進行分析,多個復制對象的指針成員只指向同一個內存區,在最后一個對象析構的時候才最終將這個內存區釋放掉。
            引用計數的好處是,可以節省內存分配、拷貝、內存釋放所帶來的效率消耗,以及節省內存。

            http://m.shnenglu.com/jake1036/archive/2011/05/17/146594.html

            淺拷貝

             1 #include <iostream>
             2 #include <cstring>
             3 using namespace std;
             4 
             5 class str
             6 {
             7 private:
             8     char* s_;
             9 public:
            10     str(const char* s = "")
            11     {
            12         s_ = new char[strlen(s) + 1];
            13         if (s_ != 0)
            14         {
            15             strcpy(s_, s);
            16         }
            17     }
            18     ~str()
            19     {
            20         delete [] s_;
            21     }
            22     char* s() const
            23     {
            24         return s_;
            25     }
            26 };
            27 
            28 ostream& operator << (ostream& outconst str& s)
            29 {
            30     out << s.s() << endl;
            31     return out;
            32 }
            33 
            34 int main()
            35 {
            36     str s1 = "123";
            37     str s2(s1);
            38     cout << s1 << endl;
            39     cout << s2 << endl;
            40 }

             


            深拷貝
             1 #include <iostream>
             2 #include <cstring>
             3 using namespace std;
             4 
             5 class str
             6 {
             7 private:
             8     char* s_;
             9 public:
            10     str(const char* s = "")
            11     {
            12         s_ = new char[strlen(s) + 1];
            13         if (s_ != 0)
            14         {
            15             strcpy(s_, s);
            16         }
            17     }
            18     str(const str& s)
            19     {
            20         s_ = new char[strlen(s.s_) + 1];
            21         if (s_ != 0)
            22         {
            23             strcpy(s_, s.s_);
            24         }
            25     }
            26     str& operator = (const str& s)
            27     {
            28         if (this != &s)
            29         {
            30             delete [] s_;
            31             s_ = new char[strlen(s.s_) + 1];
            32             if (s_ != 0)
            33             {
            34                 strcpy(s_, s.s_);
            35             }
            36         }
            37         return *this;
            38     }
            39     ~str()
            40     {
            41         delete [] s_;
            42     }
            43     char* sr() const
            44     {
            45         return s_;
            46     }
            47 };
            48 
            49 ostream& operator << (ostream& outconst str& s)
            50 {
            51     out << s.sr() << endl;
            52     return out;
            53 }
            54 
            55 int main()
            56 {
            57     str s1 = "123";
            58     str s2(s1);
            59     cout << s1 << endl;
            60     cout << s2 << endl;
            61 }


            引用計數
            引用計數的實現是通過在類對象中增加一個指向 int 型的指針,這個指針指向的那個 int 即是計數,記錄指針指向的那塊內存被幾個對象共用著。
            采用引用計數,在構造函數、析構函數、拷貝構造函數、復制運算符中,都要對這個指向 int 的指針進行操作,并且需要判斷指針指針指向 int 的變化情況,當為 0 時,則要釋放掉指針指向的內存。
             1 #include <iostream>
             2 #include <cstring>
             3 using namespace std;
             4 
             5 class str
             6 {
             7 private:
             8     char* s_;
             9     int* pcount_;
            10 public:
            11     str(const char* s = "")
            12     {
            13         s_ = new char[strlen(s) + 1];
            14         if (s_ != 0)
            15         {
            16             strcpy(s_, s);
            17             pcount_ = new int;
            18             if (pcount_ != 0)
            19             {
            20                 *pcount_ = 1;
            21             }
            22         }
            23     }
            24     str(const str& s)
            25     {
            26         s_ = s.s_;
            27         pcount_ = s.pcount_;
            28         ++(*pcount_);
            29     }
            30     str& operator = (const str& s)
            31     {
            32         if (this != &s)
            33         {
            34             --(*pcount_);
            35             if (*pcount_ == 0)
            36             {
            37                 if (s_ != 0)
            38                 {
            39                     delete [] s_;
            40                     s_ = 0;
            41                 }
            42                 delete pcount_;
            43                 pcount_ = 0;
            44             }
            45             s_ = s.s_;
            46             pcount_ = s.pcount_;
            47             ++(*pcount_);
            48         }
            49         return *this;
            50     }
            51     ~str()
            52     {
            53         --(*pcount_);
            54         if (*pcount_ == 0)
            55         {
            56             // cout << "test" << endl;
            57             if (s_ != 0)
            58             {
            59                 delete [] s_;
            60                 s_ = 0;
            61             }
            62             delete pcount_;
            63             pcount_ = 0;
            64         }
            65     }
            66     char* sr() const
            67     {
            68         return s_;
            69     }
            70 };
            71 
            72 ostream& operator << (ostream& outconst str& s)
            73 {
            74     out << s.sr() << endl;
            75     return out;
            76 }
            77 
            78 int main()
            79 {
            80     str s1 = "123";
            81     str s2(s1);
            82     cout << s1 << endl;
            83     cout << s2 << endl;
            84 }
            85 



            posted on 2011-07-22 17:20 unixfy 閱讀(266) 評論(0)  編輯 收藏 引用
            久久夜色精品国产欧美乱| 麻豆精品久久久一区二区| 色综合久久综精品| 精品国产一区二区三区久久久狼 | 久久毛片免费看一区二区三区| 久久精品a亚洲国产v高清不卡| 婷婷五月深深久久精品| 亚洲色欲久久久综合网东京热| 亚洲天堂久久久| 99久久精品国产一区二区| 亚洲va久久久噜噜噜久久男同| 久久中文字幕人妻熟av女| 老男人久久青草av高清| 中文字幕无码精品亚洲资源网久久| 欧美亚洲国产精品久久| 久久久久女人精品毛片| 国产成人AV综合久久| 久久性生大片免费观看性| 久久婷婷人人澡人人爽人人爱| 日韩精品久久久久久免费| 99久久精品国内| 精品熟女少妇aⅴ免费久久| 亚洲精品国产综合久久一线| 日韩精品久久无码中文字幕| 午夜不卡888久久| 久久99热这里只频精品6| 久久国产欧美日韩精品 | 国产精品伊人久久伊人电影| 久久久久亚洲精品无码网址| 亚洲AV无码一区东京热久久| 国产国产成人久久精品| 久久久无码精品亚洲日韩京东传媒| 久久夜色精品国产噜噜麻豆 | 久久久国产精品网站| 亚洲乱码日产精品a级毛片久久 | 国产精品久久久99| 囯产极品美女高潮无套久久久| 亚洲综合婷婷久久| 亚洲国产精品无码久久久蜜芽| 久久成人18免费网站| 久久九九亚洲精品|