詳解C++ 臨時(shí)量與臨時(shí)對(duì)象及程序的相關(guān)優(yōu)化
一、臨時(shí)量與臨時(shí)對(duì)象
臨時(shí)量:
- 內(nèi)置類型生成的臨時(shí)量是常量(臨時(shí)量,寄存器帶出來(lái))。
- 自定義類型生成的臨時(shí)量是變量 ,在內(nèi)存中。
- 隱式生成生成的臨時(shí)量是常量 ,顯式生成生成的臨時(shí)量是變量 。
臨時(shí)對(duì)象:
臨時(shí)對(duì)象是系統(tǒng)臨時(shí)分配的對(duì)象,在沒(méi)主動(dòng)聲明所需對(duì)象而又使用其功能時(shí)產(chǎn)生的
顯示對(duì)象:出現(xiàn)類型名
隱式對(duì)象:不出現(xiàn)類型名
注意: 臨時(shí)對(duì)象的生存周期只在本條語(yǔ)句,臨時(shí)對(duì)象一旦被引用,它的生存周期就和引用相同。
對(duì)象如何生成?
先分配內(nèi)存 在調(diào)用構(gòu)造函數(shù)初始化對(duì)象的成員變量 產(chǎn)生對(duì)象對(duì)象析構(gòu)了 對(duì)象就不存在了,對(duì)象的構(gòu)造和析構(gòu)是相反的。
重點(diǎn):對(duì)象生成的順序及調(diào)用的相關(guān)函數(shù)
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); 構(gòu)造t1 Test(int) // Test t5(10,10); 構(gòu)造t5 Test(int) // Test t2(20 ,20); 構(gòu)造t2 Test(const Test &) // Test t3 = t2; t2拷貝構(gòu)造t3 Test(int) // static Test t4 = Test(30,30); 構(gòu)造t4 Test(int) // t2 = Test(40,40); 構(gòu)造臨時(shí)對(duì)象 Test& operator=(const Test &) // t2 = Test(40,40); 臨時(shí)對(duì)象賦值給t2 ~Test() // t2 = Test(40,40); 析構(gòu)臨時(shí)對(duì)象 Test(int) // t2 = (Test)(40,50); 構(gòu)造臨時(shí)對(duì)象 逗號(hào)表達(dá)式 t2 = (Test)(50) 50 , 5 Test& operator=(const Test &) // t2 = (Test)(40,50); 臨時(shí)對(duì)象賦值給t2 ~Test() // t2 = (Test)(40,50);析構(gòu)臨時(shí)對(duì)象 Test(int) // t2 = 60; 構(gòu)造臨時(shí)對(duì)象 相當(dāng)于 t2 = (Test)60; Test& operator=(const Test &) // t2 = 60; 臨時(shí)對(duì)象賦值給t2 ~Test() // t2 = 60; 析構(gòu)臨時(shí)對(duì)象 Test(int) // Test *p1 = new Test; 構(gòu)造 Test Test(int) // Test *p2 = new Test[2]; 構(gòu)造 Test Test(int) // Test *p2 = new Test[2]; 構(gòu)造 Test Test(int) // Test *p3 = &Test(50,50); 構(gòu)造臨時(shí)對(duì)象 ~Test() // Test *p3 = &Test(50,50); 析構(gòu)臨時(shí)對(duì)象 Test(int) // Test &p4 = Test(50,50); 構(gòu)造臨時(shí)對(duì)象 ~Test() // 析構(gòu)p1 ~Test() // 析構(gòu)p2 ~Test() // 析構(gòu)p2 ~Test() // 析構(gòu)p4指向的臨時(shí)對(duì)象 ~Test() // 析構(gòu)t3 ~Test() // 析構(gòu)t2 ~Test() // 析構(gòu)t4 ~Test() // 析構(gòu)t5 ~Test() // 析構(gòu)t1 !
二、程序優(yōu)化
- 1.函數(shù)調(diào)用傳對(duì)象時(shí),按對(duì)象引用來(lái)傳遞,會(huì)少兩個(gè)函數(shù)
- 2.函數(shù)返回對(duì)象的時(shí)候,應(yīng)該返回一個(gè)臨時(shí)對(duì)象,不要先定義,再返回
- 3.調(diào)用返回對(duì)象的函數(shù)時(shí),應(yīng)該以初始化的方式調(diào)用,不要以賦值的方式調(diào)用
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;
}

程序分析
// 構(gòu)造t1 Test(int) // 構(gòu)造t2 Test(int) // GetTestObject(t1)實(shí)參t1通過(guò)值傳遞給Test GetTestObject(Test t) 形參 t ,調(diào)用拷貝構(gòu)造 Test(const Test &) //Test tmp(value); 構(gòu)造對(duì)象tmp Test(int) //return tmp; 將返回值tmp拷貝構(gòu)造 main函數(shù)棧棧上的臨時(shí)對(duì)象 Test(const Test&) // 析構(gòu)tmp ~Test() // 析構(gòu)形參 t ~Test() // t2 = GetTestObject(t1); 臨時(shí)對(duì)象調(diào)用賦值函數(shù)賦值給t2 operator= // 析構(gòu)臨時(shí)對(duì)象 ~Test() // 打印 ma 100 // 析構(gòu)t2 ~Test() // 析構(gòu)t1 ~Test()
優(yōu)化1:函數(shù)調(diào)用傳對(duì)象時(shí),按對(duì)象引用來(lái)傳遞,會(huì)少兩個(gè)函數(shù)
Test GetTestObject(Test &t)
{
int value = t.getData();
Test tmp(value);
return tmp;
}

優(yōu)化2:函數(shù)返回對(duì)象的時(shí)候,應(yīng)該返回一個(gè)臨時(shí)對(duì)象,不要先定義,再返回
Test GetTestObject(Test &t)
{
int value = t.getData();
/*Test tmp(value);
return tmp;*/
return Test(value);
}

優(yōu)化3:調(diào)用返回對(duì)象的函數(shù)時(shí),應(yīng)該以初始化的方式調(diào)用,不要以賦值的方式調(diào)用
int main()
{
Test t1;
Test t2 = GetTestObject(t1);
//t2 = GetTestObject(t1);
cout << t2.getData() << endl;
return 0;
}

以上所述是小編給大家介紹的C++ 臨時(shí)量與臨時(shí)對(duì)象及程序的相關(guān)優(yōu)化詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
C++實(shí)現(xiàn)主機(jī)字節(jié)序和網(wǎng)絡(luò)字節(jié)序轉(zhuǎn)換示例
這篇文章主要為大家介紹了C++實(shí)現(xiàn)主機(jī)字節(jié)序和網(wǎng)絡(luò)字節(jié)序轉(zhuǎn)換示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11
C++標(biāo)準(zhǔn)模板庫(kù)函數(shù)sort的那些事兒
sort函數(shù)是標(biāo)準(zhǔn)模板庫(kù)的函數(shù),已知開(kāi)始和結(jié)束的地址即可進(jìn)行排序,可以用于比較任何容器(必須滿足隨機(jī)迭代器),任何元素,任何條件,執(zhí)行速度一般比qsort要快2013-09-09
rapidjson解析json代碼實(shí)例以及常見(jiàn)的json core dump問(wèn)題
今天小編就為大家分享一篇關(guān)于rapidjson解析json代碼實(shí)例以及常見(jiàn)的json core dump問(wèn)題,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-04-04
C/C++?Qt?TabWidget?實(shí)現(xiàn)多窗體創(chuàng)建詳解
TabWidget組件配合自定義Dialog組件,可實(shí)現(xiàn)一個(gè)復(fù)雜的多窗體分頁(yè)結(jié)構(gòu)。這篇文章就主要介紹了如何通過(guò)TabWidget實(shí)現(xiàn)多窗體的創(chuàng)建,感興趣的小伙伴可以了解一下2021-12-12

