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

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

新聞中心

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

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

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

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

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

 
 
 
  1. cstring str = strother;

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

 
 
 
  1. str = strother;

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

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

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

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

 
 
 
  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. }

希望對你有幫助。


本文標(biāo)題:淺談C++對象的拷貝與賦值操作
網(wǎng)站路徑:http://m.5511xx.com/article/cdcccco.html