一、臨時量與臨時對象
臨時量:
內置類型生成的臨時量是常量(臨時量,寄存器帶出來)。
自定義類型生成的臨時量是變量 ,在內存中。
隱式生成生成的臨時量是常量 ,顯式生成生成的臨時量是變量 。
臨時對象:
臨時對象是系統臨時分配的對象,在沒主動聲明所需對象而又使用其功能時產生的
顯示對象:出現類型名
隱式對象:不出現類型名
注意: 臨時對象的生存周期只在本條語句,臨時對象一旦被引用,它的生存周期就和引用相同。
對象如何生成?
先分配內存 在調用構造函數初始化對象的成員變量 產生對象對象析構了 對象就不存在了,對象的構造和析構是相反的。
重點:對象生成的順序及調用的相關函數
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
!
二、程序優化
1.函數調用傳對象時,按對象引用來傳遞,會少兩個函數
2.函數返回對象的時候,應該返回一個臨時對象,不要先定義,再返回
3.調用返回對象的函數時,應該以初始化的方式調用,不要以賦值的方式調用
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;
}
本站文章版權歸原作者及原出處所有 。內容為作者個人觀點, 并不代表本站贊同其觀點和對其真實性負責,本站只提供參考并不構成任何投資及應用建議。本站是一個個人學習交流的平臺,網站上部分文章為轉載,并不用于任何商業目的,我們已經盡可能的對作者和來源進行了通告,但是能力有限或疏忽,造成漏登,請及時聯系我們,我們將根據著作權人的要求,立即更正或者刪除有關內容。本站擁有對此聲明的最終解釋權。