C++中怎么實現(xiàn)對象的拷貝與賦值操作

今天就跟大家聊聊有關(guān)C++中怎么實現(xiàn)對象的拷貝與賦值操作,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

公司主營業(yè)務:網(wǎng)站設計制作、網(wǎng)站建設、移動網(wǎng)站開發(fā)等業(yè)務。幫助企業(yè)客戶真正實現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)是一支青春激揚、勤奮敬業(yè)、活力青春激揚、勤奮敬業(yè)、活力澎湃、和諧高效的團隊。公司秉承以“開放、自由、嚴謹、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團隊有機會用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)推出績溪免費做網(wǎng)站回饋大家。

拷貝構(gòu)造函數(shù),顧名思義,等于拷貝 + 構(gòu)造。它肩負著創(chuàng)建新對象的任務,同時還要負責把另外一個對象拷貝過來。比如下面的情況就調(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ù)了。

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

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

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

#include <stdio.h>  #include <stdlib.h>  #include <string.h>  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;  }

看完上述內(nèi)容,你們對C++中怎么實現(xiàn)對象的拷貝與賦值操作有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

本文題目:C++中怎么實現(xiàn)對象的拷貝與賦值操作
瀏覽地址:http://muchs.cn/article8/jcpiop.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、動態(tài)網(wǎng)站、響應式網(wǎng)站、外貿(mào)建站、建站公司、ChatGPT

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設