一、臨時量與臨時對象
臨時量:
臨時對象:
臨時對象是系統臨時分配的對象,在沒主動聲明所需對象而又使用其功能時產生的
顯示對象:出現類型名
隱式對象:不出現類型名
注意: 臨時對象的生存周期只在本條語句,臨時對象一旦被引用,它的生存周期就和引用相同。
對象如何生成?
先分配內存 在調用構造函數初始化對象的成員變量 產生對象對象析構了 對象就不存在了,對象的構造和析構是相反的。
重點:對象生成的順序及調用的相關函數
class Test { public: Test(int a=5, int b=5):ma(a), mb(b) {cout<<"Test(int, int)"<<endl;} ~Test() {cout<<"~Test()"<<endl;} Test(const Test &src):ma(src.ma), mb(src.mb) {cout<<"Test(const Test&)"<<endl;} void operator=(const Test &src) {ma = src.ma; mb = src.mb; cout<<"operator="<<endl;} private: int ma; int mb; }; Test t1(10, 10); int main() { Test t2(20, 20); Test t3=t2; static Test t4 = Test(30, 30); t2 = Test(40, 40); t2 = (Test)(50, 50); t2 = 60; Test *p1 = new Test(70, 70); Test *p2 = new Test[2]; Test *p3 = &Test(80, 80); Test &p4 = Test(90, 90); delete p1; delete []p2; } Test t5(100, 100);
Test(int) // Test t1(10,10); 構造t1 Test(int) // Test t5(10,10); 構造t5 Test(int) // Test t2(20 ,20); 構造t2 Test(const Test &) // Test t3 = t2; t2拷貝構造t3 Test(int) // static Test t4 = Test(30,30); 構造t4 Test(int) // t2 = Test(40,40); 構造臨時對象 Test& operator=(const Test &) // t2 = Test(40,40); 臨時對象賦值給t2 ~Test() // t2 = Test(40,40); 析構臨時對象 Test(int) // t2 = (Test)(40,50); 構造臨時對象 逗號表達式 t2 = (Test)(50) 50 , 5 Test& operator=(const Test &) // t2 = (Test)(40,50); 臨時對象賦值給t2 ~Test() // t2 = (Test)(40,50);析構臨時對象 Test(int) // t2 = 60; 構造臨時對象 相當于 t2 = (Test)60; Test& operator=(const Test &) // t2 = 60; 臨時對象賦值給t2 ~Test() // t2 = 60; 析構臨時對象 Test(int) // Test *p1 = new Test; 構造 Test Test(int) // Test *p2 = new Test[2]; 構造 Test Test(int) // Test *p2 = new Test[2]; 構造 Test Test(int) // Test *p3 = &Test(50,50); 構造臨時對象 ~Test() // Test *p3 = &Test(50,50); 析構臨時對象 Test(int) // Test &p4 = Test(50,50); 構造臨時對象 ~Test() // 析構p1 ~Test() // 析構p2 ~Test() // 析構p2 ~Test() // 析構p4指向的臨時對象 ~Test() // 析構t3 ~Test() // 析構t2 ~Test() // 析構t4 ~Test() // 析構t5 ~Test() // 析構t1 !
二、程序優化
class Test { public: Test(int data = 100) :ma(data) { cout << "Test(int)" << endl; } ~Test() { cout << "~Test()" << endl; } Test(const Test &src) :ma(src.ma) { cout << "Test(const Test&)" << endl; } Test& operator=(const Test &src) { cout << "operator=" << endl; ma = src.ma; return *this; } int getData() { return ma; } private: int ma; }; Test GetTestObject(Test t) { int value = t.getData(); Test tmp(value); return tmp; //return Test(value); } int main() { Test t1; Test t2; t2 = GetTestObject(t1); cout << t2.getData() << endl; return 0; }
程序分析
// 構造t1 Test(int) // 構造t2 Test(int) // GetTestObject(t1)實參t1通過值傳遞給Test GetTestObject(Test t) 形參 t ,調用拷貝構造 Test(const Test &) //Test tmp(value); 構造對象tmp Test(int) //return tmp; 將返回值tmp拷貝構造 main函數棧棧上的臨時對象 Test(const Test&) // 析構tmp ~Test() // 析構形參 t ~Test() // t2 = GetTestObject(t1); 臨時對象調用賦值函數賦值給t2 operator= // 析構臨時對象 ~Test() // 打印 ma 100 // 析構t2 ~Test() // 析構t1 ~Test()
優化1:函數調用傳對象時,按對象引用來傳遞,會少兩個函數
Test GetTestObject(Test &t) { int value = t.getData(); Test tmp(value); return tmp; }
優化2:函數返回對象的時候,應該返回一個臨時對象,不要先定義,再返回
Test GetTestObject(Test &t) { int value = t.getData(); /*Test tmp(value); return tmp;*/ return Test(value); }
優化3:調用返回對象的函數時,應該以初始化的方式調用,不要以賦值的方式調用
int main() { Test t1; Test t2 = GetTestObject(t1); //t2 = GetTestObject(t1); cout << t2.getData() << endl; return 0; }
以上所述是小編給大家介紹的C++ 臨時量與臨時對象及程序的相關優化詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。