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

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

優(yōu)化3:調(diào)用返回對象的函數(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++ 臨時量與臨時對象及程序的相關(guān)優(yōu)化詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Java C++ 題解leetcode857雇傭K名工人最低成本vector pair
這篇文章主要為大家介紹了Java C++ 題解leetcode857雇傭K名工人最低成本vector pair示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09
C++中調(diào)用復(fù)制(拷貝)函數(shù)的三種情況總結(jié)
這篇文章主要介紹了C++中調(diào)用復(fù)制(拷貝)函數(shù)的三種情況總結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11
C++實現(xiàn)LeetCode(93.復(fù)原IP地址)
這篇文章主要介紹了C++實現(xiàn)LeetCode(93.復(fù)原IP地址),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-07-07
C語言中獲取和改變目錄的相關(guān)函數(shù)總結(jié)
這篇文章主要介紹了C語言中獲取和改變目錄的相關(guān)函數(shù)總結(jié),包括getcwd()函數(shù)和chdir()函數(shù)以及chroot()函數(shù)的使用方法,需要的朋友可以參考下2015-09-09

