C++中產(chǎn)生臨時(shí)對(duì)象的情況及其解決方案
C++中三種常見的臨時(shí)對(duì)象創(chuàng)建的情況:
(1)以值傳遞的方式給函數(shù)傳參;
(2)類型轉(zhuǎn)換;
(3)函數(shù)需要返回對(duì)象時(shí)。
1.以值傳遞的方式給函數(shù)傳參
1.1以值傳遞的方式給函數(shù)傳參
按值傳遞時(shí),傳給函數(shù)的參數(shù)會(huì)調(diào)用拷貝構(gòu)造函數(shù)創(chuàng)建一個(gè)臨時(shí)對(duì)象,所有在函數(shù)體內(nèi)的操作都是在這個(gè)臨時(shí)對(duì)象上的操作,所以函數(shù)體內(nèi)對(duì)該臨時(shí)對(duì)象進(jìn)行任何操作都不會(huì)影響原參數(shù)。當(dāng)臨時(shí)對(duì)象銷毀時(shí),即函數(shù)形參銷毀,就會(huì)調(diào)用該臨時(shí)對(duì)象的析構(gòu)函數(shù)。無論是調(diào)用拷貝構(gòu)造函數(shù)還是析構(gòu)函數(shù),都是額外的開銷。
1.2 解決方案—以引用的方式傳參
如何避免這種臨時(shí)對(duì)象的產(chǎn)生呢?
只要把以值傳遞的方式修改為以引用傳遞的方式即可。這樣既不會(huì)調(diào)用拷貝構(gòu)造函數(shù),也不會(huì)多調(diào)用一次臨時(shí)對(duì)象的析構(gòu)函數(shù)。因此能夠減少額外不必要的開銷。引用是對(duì)象本身,只是對(duì)象的一個(gè)別名。
2.類型轉(zhuǎn)換生成的臨時(shí)對(duì)象
class TempObj { public: TempObj(int a = 10, int b=20) { _capacity = a; _size = b; cout << "有參構(gòu)造" << endl; cout << "_capacity=" << _capacity << ",_size=" << _size << endl; } TempObj(const TempObj& p) { cout << "拷貝構(gòu)造" << endl; } ~TempObj() { cout << "析構(gòu)函數(shù)" << endl; } int GetSum(TempObj& p) { int sum = p._capacity + p._size; return sum; } public: int _capacity; int _size; }; int main() { TempObj m1(28,29), m2; m2 = 30; cout << "sum=" << m1.GetSum(m2) << endl; }
運(yùn)行結(jié)果如下:
main函數(shù)創(chuàng)建了兩個(gè)對(duì)象m1和m2,但輸出卻調(diào)用了三次構(gòu)造函數(shù)。關(guān)鍵在于m2=30;30和m2的類型不同,但編譯器為了通過編譯以30為參數(shù)調(diào)用構(gòu)造函數(shù)創(chuàng)建了一個(gè)臨時(shí)對(duì)象。
解決辦法如下及運(yùn)行結(jié)果如下:
此時(shí),“=”號(hào)由原本的賦值變成了構(gòu)造。對(duì)m2的構(gòu)造推遲了,當(dāng)定義TempObj m2時(shí),在main的棧中為m2對(duì)象創(chuàng)建了一個(gè)預(yù)留空間,而我們用30調(diào)用構(gòu)造函數(shù)時(shí),此時(shí)的構(gòu)造函數(shù)是在m2預(yù)留的空間中進(jìn)行的,因此減少了一次臨時(shí)對(duì)象的創(chuàng)建。
3.函數(shù)返回一個(gè)對(duì)象
一個(gè)函數(shù)若直接返回類對(duì)象,一般會(huì)生成臨時(shí)類對(duì)象變量,需多次調(diào)用拷貝構(gòu)造函數(shù)(copy constructor)造成效率低下。
3.1 需要構(gòu)造臨時(shí)對(duì)象的例子--例1
需要構(gòu)造臨時(shí)對(duì)象的一個(gè)典型的情況就是我們需要把函數(shù)的返回值賦給一個(gè)已經(jīng)存在的對(duì)象a,在這種情況下之所以不能直接使用的a的地址來存放函數(shù)f()的返回值。因?yàn)閷?duì)象a現(xiàn)在里面已經(jīng)有一些數(shù)據(jù)了,所以先要把對(duì)象a中的數(shù)據(jù)釋放掉,然后才能去接受新的數(shù)據(jù)。這種先釋放然后再拷貝的行為就是在賦值運(yùn)算符的重載里面實(shí)現(xiàn)的。
在整個(gè)過程中,f()的調(diào)用者必須要先提供一個(gè)存放函數(shù)返回值的臨時(shí)對(duì)象的地址,然后等f()返回之后,這個(gè)臨時(shí)對(duì)象中存放的就是函數(shù)返回的對(duì)象。再通過移動(dòng)的賦值將臨時(shí)對(duì)象的值賦給a。如下圖所示。
class TempObj { public: TempObj() { cout << "無參構(gòu)造函數(shù)" << endl; std::cout << "Constructor on addr:" << static_cast<const void*>(this) << std::endl; } TempObj(const TempObj& p) { cout << "拷貝構(gòu)造函數(shù)" << endl; std::cout << "Copy constructor, this addr:" << static_cast<const void*>(this) << std::endl; } //賦值運(yùn)算符重載 TempObj& operator=(const TempObj& p) { cout << "operator=(TempObj& to)" << endl; if (this != &p) { //_capacity = p._capacity; } std::cout << "Move constructor, this addr:" << static_cast<const void*>(this) << std::endl; return *this; } ~TempObj() { cout << "析構(gòu)函數(shù)" << endl; } }; TempObj GetObject() { std::cout << ">>>Enter GetObject..." << std::endl; TempObj tmp; return tmp; } int main() { TempObj m1; m1 = GetObject(); std::cout << "<<<Leave GetObject..." << std::endl; return 0; }
運(yùn)行結(jié)果如下:
首先,我們構(gòu)造了一個(gè)對(duì)象m1,m1的地址就是00000049B02FFAA4,之后調(diào)用函數(shù)GetObject(),并把函數(shù)GetObject()的返回值賦值給M1。從運(yùn)行結(jié)果可知,在GetObject()函數(shù)內(nèi)部構(gòu)造了一個(gè)新的對(duì)象即臨時(shí)對(duì)象,地址為00000049B02FFB84。這個(gè)臨時(shí)對(duì)象的空間是由函數(shù)GetObject()的調(diào)用者main函數(shù)提供的。等GetObject()調(diào)用結(jié)束,會(huì)調(diào)用賦值操作符的重載函數(shù),將臨時(shí)的對(duì)象賦值給m1。
3.2 需要構(gòu)造臨時(shí)對(duì)象的例子--例2
用圖將該情況畫出來,如下圖所示:
tmp1和tmp2都是處于函數(shù)GetObject()開辟的棧幀上面,第一步是將需要返回的對(duì)象,通過拷貝構(gòu)造函數(shù)構(gòu)造到函數(shù)GetObject()的調(diào)用者提供的存放返回值的空間上去,即(ret)的位置。第二步是調(diào)用賦值將該臨時(shí)賦值給m1。
代碼如下:
class TempObj { public: TempObj() { cout << "無參構(gòu)造函數(shù)" << endl; std::cout << "Constructor on addr:" << static_cast<const void*>(this) << std::endl; } TempObj(const TempObj& p) { cout << "拷貝構(gòu)造函數(shù)" << endl; std::cout << "Copy constructor, this addr:" << static_cast<const void*>(this) << std::endl; } //賦值運(yùn)算符重載 TempObj& operator=(const TempObj& p) { cout << "operator=(TempObj& to)" << endl; if (this != &p) { //_capacity = p._capacity; } std::cout << "Move constructor, this addr:" << static_cast<const void*>(this) << std::endl; return *this; } ~TempObj() { cout << "析構(gòu)函數(shù)" << endl; } }; TempObj GetObject(int i) { std::cout << ">>>Enter GetObject..." << std::endl; TempObj tmp1, tmp2; if (i > 0) return tmp1; else return tmp2; } int main() { TempObj m1; m1 = GetObject(1); std::cout << "<<<Leave GetObject..." << std::endl; return 0; }
運(yùn)行結(jié)果如下:
首先我們構(gòu)造一個(gè)m1,m1的地址為:000000CC1035FA54;然后進(jìn)入到函數(shù)GetObject(),該函數(shù)中會(huì)構(gòu)造兩個(gè)對(duì)象tmp1和tmp2,tmp1的地址為:000000CC1035F8F4,tmp2的地址為:000000CC1035F914。根據(jù)輸入決定哪個(gè)對(duì)象返回作為結(jié)果,然后將該結(jié)果賦值給m1。之后,調(diào)用了拷貝構(gòu)造函數(shù),這里出現(xiàn)了地址:000000CC1035FB34,該地址對(duì)應(yīng)的就是臨時(shí)對(duì)象,這個(gè)臨時(shí)對(duì)象的地址是由函數(shù)GetObject()的調(diào)用者main函數(shù)所提供的,這個(gè)地址位于main函數(shù)開辟的棧幀上,可以看到該地址和m1的地址離的比較近。
根據(jù)輸入的情況,我們把tmp1和tmp2中的1個(gè)通過拷貝構(gòu)造函數(shù)構(gòu)造出來,返回的結(jié)果通過賦值重載函數(shù)賦值到m1上,地址為:000000CC1035FA54。
3.3 返回值優(yōu)化
上圖中所說的優(yōu)化即是指C++11以及之后的C++14、17標(biāo)準(zhǔn)均提出一項(xiàng)編譯優(yōu)化技術(shù):復(fù)制省略(copy elision,也稱復(fù)制消除),這項(xiàng)優(yōu)化技術(shù)的中心思想是:一個(gè)函數(shù)若直接返回類對(duì)象,一般會(huì)生成臨時(shí)類對(duì)象變量,需多次調(diào)用拷貝構(gòu)造函數(shù)(copy constructor)造成效率低下,編譯器對(duì)此優(yōu)化,省略其中的拷貝構(gòu)造環(huán)節(jié),達(dá)到提升效率的目的。
class TempObj { public: TempObj() { cout << "無參構(gòu)造函數(shù)" << endl; std::cout << "Constructor on addr:" << static_cast<const void*>(this) << std::endl; } TempObj(const TempObj& p) { cout << "拷貝構(gòu)造函數(shù)" << endl; std::cout << "Copy constructor, this addr:" << static_cast<const void*>(this) << std::endl; } //賦值運(yùn)算符重載 TempObj& operator=(const TempObj& p) { cout << "operator=(TempObj& to)" << endl; if (this != &p) { //_capacity = p._capacity; } std::cout << "Move constructor, this addr:" << static_cast<const void*>(this) << std::endl; return *this; } ~TempObj() { cout << "析構(gòu)函數(shù)" << endl; } void Print(const char* pre) { std::cout << pre << "addr" << static_cast<const void*>(this) << std::endl; } }; TempObj GetObject() { std::cout << ">>>Enter GetObject..." << std::endl; TempObj tmp; return tmp; } int main() { TempObj m1 = GetObject(); std::cout << "<<<Leave GetObject..." << std::endl; m1.Print("Variable m1:"); return 0; }
運(yùn)行結(jié)果:
在上述代碼追蹤構(gòu)造函數(shù)和拷貝構(gòu)造函數(shù)的調(diào)用,以及構(gòu)造所發(fā)生的地址,以及提供一個(gè)打印函數(shù),來打印某一個(gè)m1對(duì)象的地址。
通過GetObject()函數(shù)構(gòu)造一個(gè)m1對(duì)象,并打印m1的地址。通過程序運(yùn)行的結(jié)果可以發(fā)現(xiàn),當(dāng)我們進(jìn)入到GetObject()函數(shù)時(shí),首先構(gòu)造了一個(gè)TempObj的對(duì)象tmp,我們會(huì)發(fā)現(xiàn)這里tmp對(duì)象所使用的地址就是最后對(duì)象m1的地址。也就是說在這整個(gè)過程里面,我們并沒有構(gòu)造一個(gè)另外的tmp對(duì)象,而是直接使用的是需要返回的m1的地址。整個(gè)過程中,并沒有調(diào)用拷貝構(gòu)造函數(shù)。這種優(yōu)化就叫Copy elision。
這種情況是直接使用函數(shù)的返回值去構(gòu)造一個(gè)新的對(duì)象的情況,這種情況下是不需要臨時(shí)對(duì)象的,因?yàn)橛行碌膶?duì)象的地址,我們可以直接使用這個(gè)地址來存放這個(gè)函數(shù)所需要返回的對(duì)象。
這種優(yōu)化不適用的情況
class TempObj { public: TempObj() { cout << "無參構(gòu)造函數(shù)" << endl; std::cout << "Constructor on addr:" << static_cast<const void*>(this) << std::endl; } TempObj(const TempObj& p) { cout << "拷貝構(gòu)造函數(shù)" << endl; std::cout << "Copy constructor, this addr:" << static_cast<const void*>(this) << std::endl; } //賦值運(yùn)算符重載 TempObj& operator=(const TempObj& p) { cout << "operator=(TempObj& to)" << endl; if (this != &p) { //_capacity = p._capacity; } std::cout << "Move constructor, this addr:" << static_cast<const void*>(this) << std::endl; return *this; } ~TempObj() { cout << "析構(gòu)函數(shù)" << endl; } void Print(const char* pre) { std::cout << pre << "addr" << static_cast<const void*>(this) << std::endl; } }; TempObj GetObject(int i) { std::cout << ">>>Enter GetObject..." << std::endl; TempObj tmp1, tmp2; if (i > 0) return tmp1; else return tmp2; } int main() { TempObj m1 = GetObject(1); std::cout << "<<<Leave GetObject..." << std::endl; m1.Print("Variable m1:"); return 0; }
運(yùn)行結(jié)果:
以上就是C++中產(chǎn)生臨時(shí)對(duì)象的情況及其解決方案的詳細(xì)內(nèi)容,更多關(guān)于C++產(chǎn)生臨時(shí)對(duì)象的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C++?超詳細(xì)分析數(shù)據(jù)結(jié)構(gòu)中的時(shí)間復(fù)雜度
時(shí)間復(fù)雜度一般指時(shí)間復(fù)雜性。?在計(jì)算機(jī)科學(xué)中,時(shí)間復(fù)雜性,又稱時(shí)間復(fù)雜度,算法的時(shí)間復(fù)雜度是一個(gè)函數(shù),它定性描述該算法的運(yùn)行時(shí)間2022-03-03QT中QByteArray與char、int、float之間的互相轉(zhuǎn)化
本文主要介紹了QT中QByteArray與char、int、float之間的互相轉(zhuǎn)化,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05C++的動(dòng)態(tài)內(nèi)存管理你真的了解嗎
這篇文章主要為大家詳細(xì)介紹了C++的動(dòng)態(tài)內(nèi)存管理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-02-02C++詳細(xì)講解內(nèi)存管理工具primitives
文章向大家介紹C++內(nèi)存管理primitives,主要包括primitives使用實(shí)例、應(yīng)用技巧、基本知識(shí)點(diǎn)總結(jié)和需要注意事項(xiàng),具有一定的參考價(jià)值,需要的朋友可以參考一下2022-06-06ShellExecute函數(shù)用法的實(shí)例代碼
ShellExecute函數(shù)用法的實(shí)例代碼,需要的朋友可以參考一下2013-03-03

Linux中使用C語言的fork()函數(shù)創(chuàng)建子進(jìn)程的實(shí)例教程