日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
淺談C++對象的拷貝與賦值操作

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

創(chuàng)新互聯(lián)是一家專業(yè)提供綿竹企業(yè)網(wǎng)站建設,專注與成都做網(wǎng)站、成都網(wǎng)站制作、H5開發(fā)、小程序制作等業(yè)務。10年已為綿竹眾多企業(yè)、政府機構等服務。創(chuàng)新互聯(lián)專業(yè)網(wǎng)絡公司優(yōu)惠進行中。

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

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

 
 
 
  1. cstring str = strother;

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

 
 
 
  1. str = strother;

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

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

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

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

 
 
 
  1. #include 
  2. #include 
  3. #include 
  4. class cstring
  5. public:
  6. cstring();
  7. cstring(const char* pszbuffer);
  8. ~cstring();
  9. cstring(const cstring& other);
  10. const cstring& operator=(const cstring& other);
  11. private:
  12. char* m_pszbuffer;;
  13. }; 
  14. cstring::cstring()
  15. {
  16. printf("cstring::cstring\n");
  17. m_pszbuffer = null;
  18. return; 
  19. cstring::cstring(const char* pszbuffer)
  20. {
  21. printf("cstring::cstring(const char* pszbuffer)\n");
  22. m_pszbuffer = pszbuffer != null ? strdup(pszbuffer) : null;
  23. return;
  24. }
  25. cstring::~cstring()
  26. {
  27. printf("%s\n", __func__);
  28. if(m_pszbuffer != null)
  29. {
  30. free(m_pszbuffer);
  31. m_pszbuffer = null;
  32. }
  33. return;
  34. }
  35. cstring::cstring(const cstring& other)
  36. {
  37. if(this == &other)
  38. {
  39. return;
  40. }
  41. printf("cstring::cstring(const cstring& other)\n");
  42. m_pszbuffer = other.m_pszbuffer != null ? strdup(other.m_pszbuffer) : null;
  43. }
  44. const cstring& cstring::operator=(const cstring& other)
  45. {
  46. printf("const cstring& cstring::operator=(const cstring& other)\n");
  47. if(this == &other)
  48. {
  49. return *this;
  50. }
  51. if(m_pszbuffer != null)
  52. {
  53. free(m_pszbuffer);
  54. m_pszbuffer = null;
  55. }
  56. m_pszbuffer = other.m_pszbuffer != null ? strdup(other.m_pszbuffer) : null;
  57. return *this;
  58. }
  59. void test(cstring str)
  60. {
  61. cstring str1 = str;
  62. return;
  63. }
  64. int main(int argc, char* argv[])
  65. {
  66. cstring str;
  67. cstring str1 = "test";
  68. cstring str2 = str1;
  69. str1 = str;
  70. cstring str3 = str3;
  71. test(str);
  72. return 0;
  73. }

希望對你有幫助。


網(wǎng)頁標題:淺談C++對象的拷貝與賦值操作
網(wǎng)站地址:http://www.5511xx.com/article/cdcccco.html