類和對象(7)—— 拷貝構造函數應用場景
阿新 • • 發佈:2018-10-16
返回值 code opera 編譯 vat test6 amp bsp 對象回收
#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; class Test { public: //顯示的無參數的構造函數 Test() { cout << "Test()..." << endl; m_x = 0; m_y = 0; } //顯式地有參數的構造函數 Test(int x, int y) { cout << "Test(int x, int y)..." << endl; m_x = x; m_y = y; } //顯式地拷貝構造函數 Test(const Test &another) { cout << "Test(const Test &)..." << endl; m_x = another.m_x; m_y = another.m_y; } void printT() { cout << "x=" << m_x << ",y=" << m_y << endl; } //顯示的拷貝析構函數 ~Test() { cout << "~Test()..." << endl; } //賦值操作符 void operator=(const Test &another) { cout << "operator=(const Test &)..." << endl; m_x = another.m_x; m_y= another.m_y; } private: int m_x; int m_y; }; //場景一 //析構函數調用的順序,跟構造相反,誰先構造,誰後析構 void test1() { cout << "test1 begin..." << endl; Test t1(10, 20); Test t2(t1);//等價於Test t2=t1; cout << "test1 end..." << endl; } //場景二 void test2() { cout << "test2 begin..." << endl; Test t1(10, 20); Test t2;//等價於Test t2=t1; t2 = t1; cout << "test2 end..." << endl; } //場景三 void func1(Test t)//Test t = t1;實際上是Test t的拷貝構造函數 { cout << "func1 begin..." << endl; t.printT(); cout << "func1 end..." << endl; } void test3() { cout << "test3 begin..." << endl; Test t1(10, 20); func1(t1); cout << "test3 end..." << endl; } Test func2() { cout << "func2 begin..." << endl; Test temp(10, 20); temp.printT(); cout << "func2 end..." << endl; return temp; }//會產生一個匿名的對象 = temp 匿名對象.拷貝構造(temp) //場景四 void test4() { cout << "test4 begin..." << endl; Test t1(10, 20); func2();//返回一個匿名對象。 //當一個函數返回匿名對象的時候,函數外部沒有任何變量去接受它, //這個匿名對象將不會再被使用(即找不到),編譯器會直接將這個匿名對象回收掉, //而不是等待整個函數執行完畢再回收。 //匿名對象此時就被回收。 cout << "test4 end..." << endl; } //場景五 void test5() { cout << "test5 begin..." << endl; Test t1 = func2();//並不會觸發t1拷貝,而是將匿名對象轉正t1, //將這個匿名對象 起了名字就叫t1。 cout << "test5 end..." << endl; } //場景六 void test6() { cout << "test6 begin..." << endl; Test t1;// t1 = func2(); t1.printT(); cout << "test6 end..." << endl; } int main(void) { test1(); cout << "-------------------" << endl; test2(); cout << "-------------------" << endl; test3(); cout << "-------------------" << endl; test4(); cout << "-------------------" << endl; test5(); cout << "-------------------" << endl; test6(); return 0; }
屏幕輸出:
test1 begin... Test(int x, int y)... Test(const Test &)... test1 end... ~Test()... ~Test()... ------------------- test2 begin... Test(int x, int y)... Test()... operator=(const Test &)... test2 end... ~Test()... ~Test()... ------------------- test3 begin... Test(int x, int y)... Test(const Test &)... func1 begin... x=10,y=20 func1 end... ~Test()... test3 end... ~Test()... ------------------- test4 begin... Test(int x, int y)... func2 begin... Test(int x, int y)... x=10,y=20 func2 end... Test(const Test &)... ~Test()... ~Test()... test4 end... ~Test()... ------------------- test5 begin... func2 begin... Test(int x, int y)... x=10,y=20 func2 end... Test(const Test &)... ~Test()... test5 end... ~Test()... ------------------- test6 begin... Test()... func2 begin... Test(int x, int y)... x=10,y=20 func2 end... Test(const Test &)... ~Test()...//析構局部元素temp operator=(const Test &)... ~Test()...//析構匿名對象 x=10,y=20 test6 end... ~Test()...
結論:
結論一:func2()函數 返回一個元素。
函數的返回值是一個元素(復雜類型的),返回的是一個新的匿名對象(所以會調用匿名對象類的拷貝構造函數);
結論二:有關 匿名函對象的去和留
如果用匿名對象 初始化 另外一個同類型的對象,匿名對象轉成有名對象;
如果用匿名對象 賦值給 另一個同類型的對象,匿名對象被析構。
類和對象(7)—— 拷貝構造函數應用場景