C++ 拷贝构造函数应用场景
生活随笔
收集整理的這篇文章主要介紹了
C++ 拷贝构造函数应用场景
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
第一個場景
#include <iostream>using namespace std;class Test{public://默認構造函數。 就是一個無參數的構造函數,//如果不顯示提供構造函數,系統就是調用默認的構造函數/*Test() {} 默認的構造函數,已經手動提供,默認就被隱藏*///如果我們提供了一個顯示的構造函數,那么默認的構造函數就被隱藏掉了。//構造函數一旦手動提供, 默認將不復存在。Test(int x, int y){cout << "-----------------------" << endl;m_x = x;m_y = y;cout << "調用了有參數的構造函數" << endl;cout << "-----------------------" << endl;}//無參數的構造函數Test() {cout << "-----------------------" << endl;m_x = 0;m_y = 0;cout << "調用了無參數的構造函數" << endl;cout << "-----------------------" << endl;}//拷貝構造函數 ,想通過另一個Test對象 another 將本對象進行拷貝Test(const Test& another){cout << "-----------------------" << endl;m_x = another.m_x;m_y = another.m_y;cout << "調用了拷貝構造函數" << endl;cout << "-----------------------" << endl;}//等號操作符void operator = (const Test& t){cout << "-----------------------" << endl;cout << "調用了=號操作符" << endl;m_x = t.m_x;m_y = t.m_y;cout << "-----------------------" << endl;}void printT(){cout << "-----------------------" << endl;cout << "x : " << m_x << ", y : " << m_y << endl;cout << "-----------------------" << endl;}//提供一個析構函數~Test(){cout << "-----------------------" << endl;cout << "~Test()析構函數被執行了" << endl;cout << "(" << m_x << ", " << m_y << ")" << "被析構了" << endl;cout << "-----------------------" << endl;}private:int m_x;int m_y; };//拷貝構造函數的第一個場景 void test1(){Test t1(1, 2); // 調用了有參數的構造函數 Test t2(t1); // 調用了拷貝構造函數//通過t1 給t2 進行賦值t2.printT(); // 打印 x, y的值// t2.printT(); 結束后,// t2 會先調用 ~Test()析構函數// t1 最后調用 ~Test()析構函數}int main(void){test1(); }第二個場景
#include <iostream>using namespace std;class Test{public://默認構造函數。 就是一個無參數的構造函數,//如果不顯示提供構造函數,系統就是調用默認的構造函數/*Test() {} 默認的構造函數,已經手動提供,默認就被隱藏*///如果我們提供了一個顯示的構造函數,那么默認的構造函數就被隱藏掉了。//構造函數一旦手動提供, 默認將不復存在。Test(int x, int y){cout << "-----------------------" << endl;m_x = x;m_y = y;cout << "調用了有參數的構造函數" << endl;cout << "-----------------------" << endl;}//無參數的構造函數Test() {cout << "-----------------------" << endl;m_x = 0;m_y = 0;cout << "調用了無參數的構造函數" << endl;cout << "-----------------------" << endl;}//拷貝構造函數 ,想通過另一個Test對象 another 將本對象進行拷貝Test(const Test& another){cout << "-----------------------" << endl;m_x = another.m_x;m_y = another.m_y;cout << "調用了拷貝構造函數" << endl;cout << "-----------------------" << endl;}//等號操作符void operator = (const Test& t){cout << "-----------------------" << endl;cout << "調用了=號操作符" << endl;m_x = t.m_x;m_y = t.m_y;cout << "-----------------------" << endl;}void printT(){cout << "-----------------------" << endl;cout << "x : " << m_x << ", y : " << m_y << endl;cout << "-----------------------" << endl;}//提供一個析構函數~Test(){cout << "-----------------------" << endl;cout << "~Test()析構函數被執行了" << endl;cout << "(" << m_x << ", " << m_y << ")" << "被析構了" << endl;cout << "-----------------------" << endl;}private:int m_x;int m_y; };//拷貝構造函數的第二場景 void test2() {Test t1(1, 2); // 調用了有參數的構造函數 Test t2; // 調用了無參數的構造函數t2 = t1; //調用的不是拷貝構造函數,調用的是 =號操作符,也能夠完成將t1的值給t2 但不是調用t2的拷貝構造函數。// 結束 // t2 會先調用 ~Test()析構函數// t1 最后調用 ~Test()析構函數 }int main(void){test2();}第三個場景
#include <iostream>using namespace std;class Test{public://默認構造函數。 就是一個無參數的構造函數,//如果不顯示提供構造函數,系統就是調用默認的構造函數/*Test() {} 默認的構造函數,已經手動提供,默認就被隱藏*///如果我們提供了一個顯示的構造函數,那么默認的構造函數就被隱藏掉了。//構造函數一旦手動提供, 默認將不復存在。Test(int x, int y){cout << "-----------------------" << endl;m_x = x;m_y = y;cout << "調用了有參數的構造函數" << endl;cout << "-----------------------" << endl;}//無參數的構造函數Test() {cout << "-----------------------" << endl;m_x = 0;m_y = 0;cout << "調用了無參數的構造函數" << endl;cout << "-----------------------" << endl;}//拷貝構造函數 ,想通過另一個Test對象 another 將本對象進行拷貝Test(const Test& another){cout << "-----------------------" << endl;m_x = another.m_x;m_y = another.m_y;cout << "調用了拷貝構造函數" << endl;cout << "-----------------------" << endl;}//等號操作符void operator = (const Test& t){cout << "-----------------------" << endl;cout << "調用了=號操作符" << endl;m_x = t.m_x;m_y = t.m_y;cout << "-----------------------" << endl;}void printT(){cout << "-----------------------" << endl;cout << "x : " << m_x << ", y : " << m_y << endl;cout << "-----------------------" << endl;}//提供一個析構函數~Test(){cout << "-----------------------" << endl;cout << "~Test()析構函數被執行了" << endl;cout << "(" << m_x << ", " << m_y << ")" << "被析構了" << endl;cout << "-----------------------" << endl;}private:int m_x;int m_y; };void func(Test t){ //Test t = test1::t1; //會調用局部變量t的拷貝構造函數/*局部創建的 Test t 在 void func(Test t) 代碼塊結束后,會被析構*/cout << "func begin..." << endl;t.printT(); // 打印 x , y 的值 cout << "func end..." << endl;}//場景三 void test3(){cout << "test3 begin ..." << endl;Test t1(10, 20); //創建了一個t1的對象。調用t1的有參數的構造函數 func(t1);cout << "test3 end..." << endl;// 結束后,調用 t1的析構函數 }int main(void){test3();}第四個場景
#include <iostream>using namespace std;class Test{public://默認構造函數。 就是一個無參數的構造函數,//如果不顯示提供構造函數,系統就是調用默認的構造函數/*Test() {} 默認的構造函數,已經手動提供,默認就被隱藏*///如果我們提供了一個顯示的構造函數,那么默認的構造函數就被隱藏掉了。//構造函數一旦手動提供, 默認將不復存在。Test(int x, int y){cout << "-----------------------" << endl;m_x = x;m_y = y;cout << "調用了有參數的構造函數" << endl;cout << "-----------------------" << endl;}//無參數的構造函數Test() {cout << "-----------------------" << endl;m_x = 0;m_y = 0;cout << "調用了無參數的構造函數" << endl;cout << "-----------------------" << endl;}//拷貝構造函數 ,想通過另一個Test對象 another 將本對象進行拷貝Test(const Test& another){cout << "-----------------------" << endl;m_x = another.m_x;m_y = another.m_y;cout << "調用了拷貝構造函數" << endl;cout << "-----------------------" << endl;}//等號操作符void operator = (const Test& t){cout << "-----------------------" << endl;cout << "調用了=號操作符" << endl;m_x = t.m_x;m_y = t.m_y;cout << "-----------------------" << endl;}void printT(){cout << "-----------------------" << endl;cout << "x : " << m_x << ", y : " << m_y << endl;cout << "-----------------------" << endl;}//提供一個析構函數~Test(){cout << "-----------------------" << endl;cout << "~Test()析構函數被執行了" << endl;cout << "(" << m_x << ", " << m_y << ")" << "被析構了" << endl;cout << "-----------------------" << endl;}private:int m_x;int m_y; };// 第四場景 Test func2() {cout << "func2 begin..." << endl;Test temp(10, 20); //調用temp的帶參數構造函數 cout << "func2 end.." << endl;return temp; // 有一個臨時的匿名對象 = temp ,把temp的數據給到了臨時的匿名對象, ,會調用這個臨時匿名//對象的拷貝構造函數, 將temp穿進去。 }void test4(){cout << "test4 begin " << endl;func2();//匿名對象在此被析構了, 如果一個臨時的匿名對象,沒有任何變量去接收它,編譯器認為這個臨時匿名對象沒有用處。//編譯器會立刻銷毀這個臨時的匿名對象cout << "test4 end" << endl; }int main(void){test4();}第五個場景
#include <iostream>using namespace std;class Test{public://默認構造函數。 就是一個無參數的構造函數,//如果不顯示提供構造函數,系統就是調用默認的構造函數/*Test() {} 默認的構造函數,已經手動提供,默認就被隱藏*///如果我們提供了一個顯示的構造函數,那么默認的構造函數就被隱藏掉了。//構造函數一旦手動提供, 默認將不復存在。Test(int x, int y){cout << "-----------------------" << endl;m_x = x;m_y = y;cout << "調用了有參數的構造函數" << endl;cout << "-----------------------" << endl;}//無參數的構造函數Test() {cout << "-----------------------" << endl;m_x = 0;m_y = 0;cout << "調用了無參數的構造函數" << endl;cout << "-----------------------" << endl;}//拷貝構造函數 ,想通過另一個Test對象 another 將本對象進行拷貝Test(const Test& another){cout << "-----------------------" << endl;m_x = another.m_x;m_y = another.m_y;cout << "調用了拷貝構造函數" << endl;cout << "-----------------------" << endl;}//等號操作符void operator = (const Test& t){cout << "-----------------------" << endl;cout << "調用了=號操作符" << endl;m_x = t.m_x;m_y = t.m_y;cout << "-----------------------" << endl;}void printT(){cout << "-----------------------" << endl;cout << "x : " << m_x << ", y : " << m_y << endl;cout << "-----------------------" << endl;}//提供一個析構函數~Test(){cout << "-----------------------" << endl;cout << "~Test()析構函數被執行了" << endl;cout << "(" << m_x << ", " << m_y << ")" << "被析構了" << endl;cout << "-----------------------" << endl;}private:int m_x;int m_y; };Test func2(){cout << "func2 begin..." << endl;Test temp(10, 20); //調用temp的帶參數構造函數 cout << "func2 end.." << endl;return temp; // 有一個臨時的匿名對象 = temp ,把temp的數據給到了臨時的匿名對象, ,會調用這個臨時匿名//對象的拷貝構造函數, 將temp穿進去。 }// 第五場景 void test5(){cout << "test5 begin ..." << endl;Test t1 = func2();//如果有一個變量去接收這個臨時的匿名對象, 編譯器認為這個匿名對象轉正了,就不會立刻給他銷毀。//t1 = 匿名的臨時對象 為什么不會發生拷貝構造// 此時的t1 去接收這個匿名的臨時對象不是 重新創建一個t1 而是給這個匿名對象起個名字就叫t1//一旦這個匿名對象有了自己的名字,編譯器就不會立刻給這個匿名對象銷毀了,//就當普通局部變量處理了cout << "test5 end..." << endl;//在此時析構的t1 }int main(void){test5(); }第六個場景
#include <iostream>using namespace std;class Test{public://默認構造函數。 就是一個無參數的構造函數,//如果不顯示提供構造函數,系統就是調用默認的構造函數/*Test() {} 默認的構造函數,已經手動提供,默認就被隱藏*///如果我們提供了一個顯示的構造函數,那么默認的構造函數就被隱藏掉了。//構造函數一旦手動提供, 默認將不復存在。Test(int x, int y){cout << "-----------------------" << endl;m_x = x;m_y = y;cout << "調用了有參數的構造函數" << endl;cout << "-----------------------" << endl;}//無參數的構造函數Test() {cout << "-----------------------" << endl;m_x = 0;m_y = 0;cout << "調用了無參數的構造函數" << endl;cout << "-----------------------" << endl;}//拷貝構造函數 ,想通過另一個Test對象 another 將本對象進行拷貝Test(const Test& another){cout << "-----------------------" << endl;m_x = another.m_x;m_y = another.m_y;cout << "調用了拷貝構造函數" << endl;cout << "-----------------------" << endl;}//等號操作符void operator = (const Test& t){cout << "-----------------------" << endl;cout << "調用了=號操作符" << endl;m_x = t.m_x;m_y = t.m_y;cout << "-----------------------" << endl;}void printT(){cout << "-----------------------" << endl;cout << "x : " << m_x << ", y : " << m_y << endl;cout << "-----------------------" << endl;}//提供一個析構函數~Test(){cout << "-----------------------" << endl;cout << "~Test()析構函數被執行了" << endl;cout << "(" << m_x << ", " << m_y << ")" << "被析構了" << endl;cout << "-----------------------" << endl;}private:int m_x;int m_y; };Test func2(){cout << "func2 begin..." << endl;Test temp(10, 20); //調用temp的帶參數構造函數 cout << "func2 end.." << endl;return temp; // 有一個臨時的匿名對象 = temp ,把temp的數據給到了臨時的匿名對象, ,會調用這個臨時匿名//對象的拷貝構造函數, 將temp穿進去。 }// 第六場景 void test6(){cout << "test6 begin..." << endl;Test t1; //調用t1的無參數構造函數t1 = func2(); //調用的=號操作符 ,,t1 = 匿名對象。 調用了t1的=號操作符。//此時匿名沒有被轉正,匿名沒有自己的名字, 匿名對象這個內存沒有自己的別名, 編譯器就會立刻銷毀。cout << "test6 end..." << endl; }int main(void){test6(); }總結
以上是生活随笔為你收集整理的C++ 拷贝构造函数应用场景的全部內容,希望文章能夠幫你解決所遇到的問題。