自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

關(guān)于C++默認(rèn)拷貝構(gòu)造函數(shù)產(chǎn)生的問題的討論

開發(fā) 后端
對于拷貝構(gòu)造函數(shù),我前面的博文有提起過,不過,有的時(shí)候,淺拷貝和深拷貝真的很難理解,所以,我們再進(jìn)行關(guān)于拷貝構(gòu)造函數(shù)的一些討論。

對于拷貝構(gòu)造函數(shù),我前面的博文有提起過,不過,有的時(shí)候,淺拷貝和深拷貝真的很難理解,所以,我們再進(jìn)行關(guān)于拷貝構(gòu)造函數(shù)的一些討論。
 

大家都整到拷貝構(gòu)造函數(shù)分為淺拷貝深拷貝,系統(tǒng)默認(rèn)的拷貝構(gòu)造函數(shù)是淺拷貝。

默認(rèn)拷貝構(gòu)造函數(shù)以內(nèi)存拷貝的方式將舊有對象的內(nèi)存空間拷貝到新對象的內(nèi)存空間。

如果類中有指針類型的類型的時(shí)候,默認(rèn)拷貝構(gòu)造函數(shù)只能復(fù)制指針屬性的值,而不能復(fù)制指針屬性所指向的內(nèi)存,此時(shí),如果我們自己不顯式定義拷貝構(gòu)造函數(shù),那么我們在編程的時(shí)候,可能會出現(xiàn)很詭異的問題。

顯式定義拷貝構(gòu)造函數(shù)來完成指針屬性等需要特殊處理的屬性的拷貝工作。

The Number one :  我們先來看淺拷貝帶來的問題

---------------------我是分割線------------------------

  1. # include <iostream>   
  2. using namespace std;   
  3.     
  4. class Car   
  5. {   
  6. private:   
  7.     char*  brand;   
  8.     float  price;   
  9. public:   
  10.     Car(const char* sz, float p)          
  11.     {   
  12.         //構(gòu)造函數(shù)中為brand分配內(nèi)存   
  13.         brand = new char[strlen(sz)+1];   
  14.         strcpy(brand, sz);   
  15.     }   
  16.     ~Car   
  17.     {   
  18.         //析構(gòu)函數(shù)中釋放申請的內(nèi)存   
  19.         delete[] brand;   
  20.         cout << " Clear is over ! " << endl;           
  21.     }   
  22.     void just_print()   
  23.     {   
  24.         cout << "brand : " << brand << endl;   
  25.         cout << "price : " << price << endl;   
  26.     }   
  27. };   
  28.     
  29. int main(void)   
  30. {   
  31.     Car car_one("BMW",120);   
  32.     car_one.just_print();   
  33.     //調(diào)用默認(rèn)的拷貝構(gòu)造函數(shù)   
  34.     Car car_two(comp_one);   
  35.     car_two.print();   
  36.     
  37.     return 0;   

----------------------------------------------------------------------------
 

這個(gè)程序運(yùn)行失敗,代碼分析:

1、car_two(car_one)等價(jià)于

         car_two.brand = car_one.brand;

         car_two.price  = car_one.price;

2、經(jīng)過賦值操作后,兩個(gè)對象中的指針指向的是同一塊動態(tài)內(nèi)存,當(dāng)car_one和car_two撤銷時(shí),其釋放函數(shù)都要釋放同一塊動態(tài)內(nèi)存內(nèi)存,可是,兩個(gè)對象撤銷有先有后,一旦一個(gè)對象被撤銷,另一個(gè)對象的brand指針變速"野指針",使用該指針再次釋放同一塊動態(tài)內(nèi)存會引發(fā)內(nèi)存錯(cuò)誤。

不僅僅是重復(fù)釋放內(nèi)存的問題,還會出現(xiàn)其他問題:

-------------------------------------------------------------------------------

  1. int main(void)   
  2. {   
  3.     Car car_one("Dell", 7000);   
  4.         
  5.     if(true)   
  6.     {   
  7.         car car_two(car_one);   
  8.         car_two.print();   
  9.     }   
  10.     //car_one.brand指向的動態(tài)內(nèi)存此時(shí)已經(jīng)被釋放   
  11.     car_one.print();   
  12.         
  13.     return 0;   

-------------------------------------------------------------------------------------------

由于car_two是在if結(jié)構(gòu)中定義的局部對象,因此if結(jié)構(gòu)退出時(shí),car_two被撤銷,系統(tǒng)自動調(diào)用其析構(gòu)函數(shù),釋放了car_two.brand所指向的動態(tài)內(nèi)存,由于car_one和car_two值相同,此時(shí)car_one.brand已無所指,成了野指針,此時(shí),對該指針的讀寫操作都會引發(fā)無法預(yù)料的錯(cuò)誤。

----------------------------------------------------------------------------

此時(shí),我們就需要自己來定義拷貝構(gòu)造函數(shù):

----------------------------------------------------------------------------

  1. //顯式定義構(gòu)造函數(shù)   
  2. # include <iostream>   
  3. # include <cstring>   
  4. using namespace std;   
  5.     
  6. class Car   
  7. {   
  8. private:   
  9.     char*    brand;   
  10.     float    price;   
  11. public:   
  12.     Car(const char*  sz, float  p)       
  13.     {   
  14.     brand = new char[strlen(sz)+1];   
  15.         strcpy(brand, sz);   
  16.         price = p;           
  17.     }   
  18.     //自定義拷貝構(gòu)造函數(shù)   
  19.     Car(const  Car&  cp)   
  20.     {   
  21.     //重新為brand開辟與cp.brand同等大小的內(nèi)存空間   
  22.     brand = new char[strlen(cp.brand) + 1];   
  23.         //   
  24.         strcpy(brand, cp.brand);   
  25.         price = cp.price;           
  26.     }   
  27.     ~Car()   
  28.     {   
  29.     delete[]  brand;   
  30.         cout << "clear over " <<endl;          
  31.     }   
  32.         
  33.     void print()   
  34.     {   
  35.     cout << "brand " << endl;   
  36.         cout << "price " << endl;    
  37.     }       
  38. };   
  39.     
  40. int main(void)   
  41. {   
  42.     Car car_one("Dell", 8999);   
  43.     car_one.print();   
  44.     //   
  45.     Car car_two(car_one);   
  46.     car_two.print();   
  47.     //沒有采用brand = cp.brand 這種直接直接賦值,而是重新申請動態(tài)內(nèi)存,使用   
  48.     //庫函數(shù)strcpy實(shí)現(xiàn)了字符串的復(fù)制   
  49.     //car_one.brand和car_two.brand指向兩塊不同的內(nèi)存,避免了錯(cuò)誤   
  50.              
  51.     return 0;   

------------------------------------------------------------------------------------------

***提一點(diǎn),自定義的拷貝構(gòu)造函數(shù),***也重載operator=運(yùn)算符!

-----------------------------------------------------------------------------------------

51cto博客:http://liam2199.blog.51cto.com/2879872/1417892

責(zé)任編輯:林師授 來源: 51cto博客
相關(guān)推薦

2025-02-06 13:23:09

C++函數(shù)參數(shù)

2011-07-20 13:40:09

拷貝構(gòu)造函數(shù)

2010-01-27 17:16:52

C++構(gòu)造函數(shù)

2024-01-25 11:36:08

C++構(gòu)造函數(shù)關(guān)鍵字

2024-04-28 11:01:27

C++編程語言函數(shù)

2024-12-06 12:00:00

C++構(gòu)造函數(shù)

2010-01-25 14:43:00

C++構(gòu)造函數(shù)

2010-01-25 17:05:37

C++語言

2009-08-13 17:30:30

C#構(gòu)造函數(shù)

2010-01-28 10:49:22

C++構(gòu)造函數(shù)

2023-11-28 11:51:01

C++函數(shù)

2010-07-13 15:36:33

2011-08-15 17:29:36

Objective-C構(gòu)造函數(shù)

2010-01-22 11:13:16

C++靜態(tài)

2010-01-27 10:13:22

C++類對象

2010-01-27 16:10:32

C++靜態(tài)構(gòu)造函數(shù)

2010-01-25 14:00:27

C++類

2009-07-31 14:47:22

JavaScript函C#

2010-02-01 11:01:30

C++靜態(tài)構(gòu)造函數(shù)

2009-11-17 09:07:55

靜態(tài)構(gòu)造函數(shù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號