• <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>
            C++分析研究  
            C++
            日歷
            <2011年9月>
            28293031123
            45678910
            11121314151617
            18192021222324
            2526272829301
            2345678
            統計
            • 隨筆 - 92
            • 文章 - 4
            • 評論 - 4
            • 引用 - 0

            導航

            常用鏈接

            留言簿

            隨筆檔案

            文章檔案

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

             

              在用C++編寫一個類時,知道什么時候需要實現拷貝構造函數和賦值操作,但不知道什么時候拷貝構造函數被調用,什么時候賦值操作被調用,甚至把二者混為一談。

              要弄明白這個問題,最簡單的做法莫過于寫個測試程序試一下。不過那樣做也未必是好辦法,實驗的結果往往導致以偏概全的結論。不如好好想一下,弄清楚其中的原理,再去寫程序去驗證也不遲。

              拷貝構造函數,顧名思義,等于拷貝 + 構造。它肩負著創建新對象的任務,同時還要負責把另外一個對象拷貝過來。比如下面的情況就調用拷貝構造函數:

              cstring str = strother;

              賦值操作則只含有拷貝的意思,也就是說對象必須已經存在。比如下面的情況會調用賦值操作。

              str = strother;

              不過有的對象是隱式的,由編譯器產生的代碼創建,比如函數以傳值的方式傳遞一個對象時。由于看不見相關代碼,所以不太容易明白。不過我們稍微思考一下,就會想到,既然是根據一個存在的對象拷貝生成新的對象,自然是調用拷貝構造函數了。

              兩者實現時有什么差別呢?我想有人會說,沒有差別。呵,如果沒有差別,那么只要實現其中一個就行了,何必要兩者都實現呢?不繞圈子了,它們的差別是:

              拷貝構造函數對同一個對象來說只會調用一次,而且是在對象構造時調用。此時對象本身還沒有構造,無需要去釋放自己的一些資源。而賦值操作可能會調用多次,你在拷貝之前要釋放自己的一些資源,否則會造成資源泄露。

              明白了這些道理之后,我們不防寫個測試程序來驗證一下我們的想法:

              #include

              #include

              #include

              class cstring

              {

              public:

              cstring();

              cstring(const char* pszbuffer);

              ~cstring();

              cstring(const cstring& other);

              const cstring& operator=(const cstring& other);

              private: char* m_pszbuffer;;

              };

              cstring::cstring()

              {

              printf("cstring::cstring\n");

              m_pszbuffer = null;

              return;

              }

              cstring::cstring(const char* pszbuffer)

              {

              printf("cstring::cstring(const char* pszbuffer)\n");

              m_pszbuffer = pszbuffer != null ? strdup(pszbuffer) : null;

              return;

              }

              cstring::~cstring()

              {

              printf("%s\n", __func__);

              if(m_pszbuffer != null)

              {

              free(m_pszbuffer);

              m_pszbuffer = null;

              }

              return;

              }

              cstring::cstring(const cstring& other)

              {

              if(this == &other)

              {

              return;

              }

              printf("cstring::cstring(const cstring& other)\n");

              m_pszbuffer = other.m_pszbuffer != null ? strdup(other.m_pszbuffer) : null;

              }

              const cstring& cstring::operator=(const cstring& other)

              {

              printf("const cstring& cstring::operator=(const cstring& other)\n");

              if(this == &other)

              {

              return *this;

              }

              if(m_pszbuffer != null)

              {

              free(m_pszbuffer);

              m_pszbuffer = null;

              }

              m_pszbuffer = other.m_pszbuffer != null ? strdup(other.m_pszbuffer) : null;

              return *this;

              }

              void test(cstring str)

              {

              cstring str1 = str;

              return;

              }

              int main(int argc, char* argv[])

              {

              cstring str;

              cstring str1 = "test";

              cstring str2 = str1;

              str1 = str;

              cstring str3 = str3;

              test(str);

              return 0;

              }

            posted on 2011-09-04 22:57 HAOSOLA 閱讀(224) 評論(0)  編輯 收藏 引用
             
            Copyright © HAOSOLA Powered by: 博客園 模板提供:滬江博客
            PK10開獎 PK10開獎
            日本三级久久网| 狠狠狠色丁香婷婷综合久久五月 | 久久综合综合久久综合| 麻豆成人久久精品二区三区免费 | 亚洲欧洲精品成人久久曰影片| 中文字幕无码久久精品青草| 精品久久久久久国产| 久久精品一区二区三区中文字幕| 久久AV高清无码| 亚洲人成精品久久久久| 亚洲伊人久久综合中文成人网| 久久人人添人人爽添人人片牛牛| 国产精品青草久久久久婷婷| 亚洲国产精品无码久久久秋霞2| 久久ZYZ资源站无码中文动漫| 伊人久久亚洲综合影院| 久久精品亚洲精品国产色婷| 国内精品久久久久久久亚洲| 久久不射电影网| 国产精品中文久久久久久久| 国产欧美久久久精品影院| 97精品久久天干天天天按摩| 国产精品美女久久福利网站| 亚洲伊人久久大香线蕉苏妲己| 久久国产精品久久国产精品| 久久99精品久久久大学生| 久久久久一级精品亚洲国产成人综合AV区 | 国产福利电影一区二区三区久久老子无码午夜伦不 | 伊人久久综合无码成人网| 欧美精品乱码99久久蜜桃| 久久精品成人免费看| 亚洲国产一成人久久精品| 热综合一本伊人久久精品| 色婷婷噜噜久久国产精品12p| AV色综合久久天堂AV色综合在| 91麻豆国产精品91久久久| 精品久久久久久无码人妻蜜桃| MM131亚洲国产美女久久| 2022年国产精品久久久久| 久久偷看各类wc女厕嘘嘘| 精品多毛少妇人妻AV免费久久|