• <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
            統(tǒng)計(jì)
            • 隨筆 - 92
            • 文章 - 4
            • 評論 - 4
            • 引用 - 0

            導(dǎo)航

            常用鏈接

            留言簿

            隨筆檔案

            文章檔案

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

             

              在用C++編寫一個類時,知道什么時候需要實(shí)現(xiàn)拷貝構(gòu)造函數(shù)和賦值操作,但不知道什么時候拷貝構(gòu)造函數(shù)被調(diào)用,什么時候賦值操作被調(diào)用,甚至把二者混為一談。

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

              拷貝構(gòu)造函數(shù),顧名思義,等于拷貝 + 構(gòu)造。它肩負(fù)著創(chuàng)建新對象的任務(wù),同時還要負(fù)責(zé)把另外一個對象拷貝過來。比如下面的情況就調(diào)用拷貝構(gòu)造函數(shù):

              cstring str = strother;

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

              str = strother;

              不過有的對象是隱式的,由編譯器產(chǎn)生的代碼創(chuàng)建,比如函數(shù)以傳值的方式傳遞一個對象時。由于看不見相關(guān)代碼,所以不太容易明白。不過我們稍微思考一下,就會想到,既然是根據(jù)一個存在的對象拷貝生成新的對象,自然是調(diào)用拷貝構(gòu)造函數(shù)了。

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

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

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

              #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 閱讀(221) 評論(0)  編輯 收藏 引用

            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


             
            Copyright © HAOSOLA Powered by: 博客園 模板提供:滬江博客
            PK10開獎 PK10開獎
            人人狠狠综合久久亚洲婷婷| 国产精品免费久久久久电影网| 日本欧美国产精品第一页久久| 国产农村妇女毛片精品久久| 久久精品夜色噜噜亚洲A∨| 久久亚洲天堂| 久久亚洲日韩精品一区二区三区| 久久精品亚洲中文字幕无码麻豆| 97久久精品人人做人人爽| 婷婷久久综合九色综合绿巨人| 午夜天堂精品久久久久| 久久成人精品视频| 久久久久亚洲AV成人网人人网站| 国产精品久久网| 久久99久久成人免费播放| 久久99亚洲网美利坚合众国| 成人精品一区二区久久久| 亚洲精品视频久久久| 久久国产亚洲精品无码| 久久亚洲国产精品五月天婷| 久久久久国产精品熟女影院| 久久久久国产一区二区| 成人免费网站久久久| 久久久亚洲欧洲日产国码是AV | 97精品国产97久久久久久免费| 久久久久亚洲国产| 国产精品99久久不卡| 久久精品亚洲一区二区三区浴池| 色妞色综合久久夜夜| 久久涩综合| 精品99久久aaa一级毛片| 久久精品国产91久久麻豆自制| 久久人人添人人爽添人人片牛牛| 久久人妻少妇嫩草AV无码蜜桃| 国产精品久久久99| 久久精品国产精品亚洲精品 | 久久国产精品国语对白| 久久99热国产这有精品| 国产精品久久波多野结衣| 久久亚洲国产成人精品性色| 久久人人爽人人爽人人片AV高清|