新聞中心
在C++存在拷貝構造函數,拷貝構造函數與不同構造函數形成重載(這一點很重要),這就意味著(要么class入口為普通構造函數,要么為拷貝構造函數,不可能2個都會執(zhí)行的)。好了 , 下面可是今天的Studying
公司主營業(yè)務:網站設計制作、網站制作、移動網站開發(fā)等業(yè)務。幫助企業(yè)客戶真正實現(xiàn)互聯(lián)網宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)是一支青春激揚、勤奮敬業(yè)、活力青春激揚、勤奮敬業(yè)、活力澎湃、和諧高效的團隊。公司秉承以“開放、自由、嚴謹、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領域給我們帶來的挑戰(zhàn),讓我們激情的團隊有機會用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)推出象州免費做網站回饋大家。
一 , 實際上C++類中有一個默認的拷貝構造,它的作用是將此類中非static成員逐一copy。現(xiàn)在先不管默認的Copy構造,我先先重載一下Copy構造:
#includeusing namespace std; class copyC { public: int a; copyC() { this->a = 2; } //拷貝構造函數 copyC( const copyC &b ) { //執(zhí)行拷貝構造函數 this->a = b.a; } }; int main() { return 0; }
拷貝函數 : copyC( const copyC &b ) , 里面的語句:this->a = b.a;實現(xiàn)的效果其實是和默認拷貝構造是一樣的。
話鋒先轉一下,談下拷貝函數的觸發(fā)機制(在什么條件下會調用)
①:用一個對象為另一個對象初始化
1->
#includeusing namespace std; class copyC { public: int a; copyC() { this->a = 2; } //拷貝構造函數 copyC( const copyC &b ) { //執(zhí)行拷貝構造函數 cout << "執(zhí)行了構造函數" << endl; this->a = b.a; } }; int main() { copyC str; copyC new1(str); cout << new1.a << endl; return 0; }
結果:
關鍵:
copyC str;
copyC new1(str);
2->
copyC str; copyC new1 = str;
3->
copyC str; copyC new1 = copyC(str);
4->
#includeusing namespace std; class copyC { public: int a; copyC() { this->a = 2; } //拷貝構造函數 copyC( const copyC &b ) { //執(zhí)行拷貝構造函數 cout << "執(zhí)行了構造函數" << endl; this->a = b.a; } }; int main() { copyC str; copyC *new1 = new copyC(str); cout << new1->a << endl; delete new1; return 0; }
結果一樣:
小結 :
上面的1,2,3,4中情況都會調用拷貝構造。
②:當對象生成對象副本時
1->傳遞對象
#includeusing namespace std; class copyC { public: int a; copyC() { this->a = 2; } //拷貝構造函數 copyC( const copyC &b ) { //執(zhí)行拷貝構造函數 cout << "執(zhí)行了構造函數" << endl; this->a = b.a; } static void copyFun( copyC b ) { } }; int main() { copyC str; copyC::copyFun(str); return 0; }
結果:
2->返回對象
#includeusing namespace std; class copyC { public: int a; copyC() { this->a = 2; } //拷貝構造函數 copyC( const copyC &b ) { //執(zhí)行拷貝構造函數 cout << "執(zhí)行了構造函數" << endl; this->a = b.a; } static void copyFun( copyC b ) { } copyC copyFun() { copyC a; return a; } }; int main() { copyC str; copyC new1 = str.copyFun(); new1.a = 4; cout << str.a << endl; cout << new1.a << endl; return 0; }
結果有點詭異 , 但是理論是正確的:
new1確實拷貝成功 , 但是“執(zhí)行了構造函數”好像沒打印出來,這和IDE有關嗎 ? 各位讀者
好了 , 現(xiàn)在正式講講拷貝構造的作用:
默認拷貝為淺拷貝,淺拷貝在有*(指針)成員的時候會報錯:
#includeusing namespace std; class copyC { public: int *a; copyC() { this->a = new int[2]; *(a) = 1; *(a+1) = 2; } //拷貝構造函數 copyC( const copyC &b ) { //執(zhí)行拷貝構造函數 cout << "執(zhí)行了構造函數" << endl; this->a = b.a; } ~copyC() { delete[] a; } }; int main() { { copyC str; cout << str.a[0] << " " << str.a[1] << endl; copyC new1 = str; cout << new1.a[0] << " " << new1.a[1] << endl; } return 0; }
出現(xiàn)結果:
正確的結果出來了 , 但是bug也出來了 。
對于bug的解釋:
無論是str還是new1對象,他們的成員指針a都是1個對象(不是2個相同的對象),在內存中的一個對象,那么一個指針是不能釋放2次的。
一,如下:
#includeusing namespace std; class copyC { public: int *a; copyC() { this->a = new int[2]; *(a) = 1; *(a+1) = 2; } //拷貝構造函數 copyC( const copyC &b ) { //執(zhí)行拷貝構造函數 cout << "執(zhí)行了構造函數" << endl; this->a = b.a; } ~copyC() { if( a != NULL ) delete[] a; } }; int main() { { copyC str; cout << str.a[0] << " " << str.a[1] << endl; copyC new1 = str; cout << new1.a[0] << " " << new1.a[1] << endl; } return 0; }
使用這種方案的時候一定要注意 : a(指針)雖然在2個對象里面(一個是copy的對象)但是a確實是1個對象。
二,重載拷貝構造(這個可以將a(指針)在確實的拷貝一份,這就是深拷貝了)
#includeusing namespace std; class copyC { public: int *a; copyC() { this->a = new int[2]; *(a) = 1; *(a+1) = 2; } //拷貝構造函數 copyC( const copyC &b ) { //執(zhí)行拷貝構造函數 cout << "執(zhí)行了構造函數" << endl; //this->a = b.a;//這是淺拷貝方案 this->a = new int[2]; memcpy(this->a , b.a , 2*sizeof(int)); } ~copyC() { delete[] a; } }; int main() { system("color 2B"); { copyC str; copyC new1 = str; cout << new1.a[0] << " " << new1.a[1] << endl; new1.a[0] = 3; new1.a[1] = 4; cout << "--------------------------------------------" << endl; cout << new1.a[0] << " " << new1.a[1] << endl; cout << str.a[0] << " " << str.a[1] << endl; } return 0; }
運行結果:
str 中的成員a 和 new1中的成員a ,確實是2個對象哈。。。。。。
好了 , 本篇結束。。。。
本文標題:C++拷貝構造
本文鏈接:http://fisionsoft.com.cn/article/gpecde.html