亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C++中產(chǎn)生臨時對象的情況及其解決方案

 更新時間:2024年05月28日 10:01:18   作者:weixin_44902783  
這篇文章主要介紹了C++中產(chǎn)生臨時對象的情況及其解決方案,以值傳遞的方式給函數(shù)傳參,類型轉(zhuǎn)換以及函數(shù)需要返回對象時,并給對應(yīng)給出了詳細(xì)的解決方案,通過圖文結(jié)合的方式講解的非常詳細(xì),需要的朋友可以參考下

C++中三種常見的臨時對象創(chuàng)建的情況:

(1)以值傳遞的方式給函數(shù)傳參;

(2)類型轉(zhuǎn)換;

(3)函數(shù)需要返回對象時。

1.以值傳遞的方式給函數(shù)傳參

1.1以值傳遞的方式給函數(shù)傳參

按值傳遞時,傳給函數(shù)的參數(shù)會調(diào)用拷貝構(gòu)造函數(shù)創(chuàng)建一個臨時對象,所有在函數(shù)體內(nèi)的操作都是在這個臨時對象上的操作,所以函數(shù)體內(nèi)對該臨時對象進(jìn)行任何操作都不會影響原參數(shù)。當(dāng)臨時對象銷毀時,即函數(shù)形參銷毀,就會調(diào)用該臨時對象的析構(gòu)函數(shù)。無論是調(diào)用拷貝構(gòu)造函數(shù)還是析構(gòu)函數(shù),都是額外的開銷。

1.2 解決方案—以引用的方式傳參

如何避免這種臨時對象的產(chǎn)生呢?

只要把以值傳遞的方式修改為以引用傳遞的方式即可。這樣既不會調(diào)用拷貝構(gòu)造函數(shù),也不會多調(diào)用一次臨時對象的析構(gòu)函數(shù)。因此能夠減少額外不必要的開銷。引用是對象本身,只是對象的一個別名。

2.類型轉(zhuǎn)換生成的臨時對象

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)建了兩個對象m1和m2,但輸出卻調(diào)用了三次構(gòu)造函數(shù)。關(guān)鍵在于m2=30;30和m2的類型不同,但編譯器為了通過編譯以30為參數(shù)調(diào)用構(gòu)造函數(shù)創(chuàng)建了一個臨時對象。

解決辦法如下及運(yùn)行結(jié)果如下:

此時,“=”號由原本的賦值變成了構(gòu)造。對m2的構(gòu)造推遲了,當(dāng)定義TempObj m2時,在main的棧中為m2對象創(chuàng)建了一個預(yù)留空間,而我們用30調(diào)用構(gòu)造函數(shù)時,此時的構(gòu)造函數(shù)是在m2預(yù)留的空間中進(jìn)行的,因此減少了一次臨時對象的創(chuàng)建。

3.函數(shù)返回一個對象

一個函數(shù)若直接返回類對象,一般會生成臨時類對象變量,需多次調(diào)用拷貝構(gòu)造函數(shù)(copy constructor)造成效率低下。

3.1 需要構(gòu)造臨時對象的例子--例1

需要構(gòu)造臨時對象的一個典型的情況就是我們需要把函數(shù)的返回值賦給一個已經(jīng)存在的對象a,在這種情況下之所以不能直接使用的a的地址來存放函數(shù)f()的返回值。因?yàn)閷ο骯現(xiàn)在里面已經(jīng)有一些數(shù)據(jù)了,所以先要把對象a中的數(shù)據(jù)釋放掉,然后才能去接受新的數(shù)據(jù)。這種先釋放然后再拷貝的行為就是在賦值運(yùn)算符的重載里面實(shí)現(xiàn)的。

在整個過程中,f()的調(diào)用者必須要先提供一個存放函數(shù)返回值的臨時對象的地址,然后等f()返回之后,這個臨時對象中存放的就是函數(shù)返回的對象。再通過移動的賦值將臨時對象的值賦給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)造了一個對象m1,m1的地址就是00000049B02FFAA4,之后調(diào)用函數(shù)GetObject(),并把函數(shù)GetObject()的返回值賦值給M1。從運(yùn)行結(jié)果可知,在GetObject()函數(shù)內(nèi)部構(gòu)造了一個新的對象即臨時對象,地址為00000049B02FFB84。這個臨時對象的空間是由函數(shù)GetObject()的調(diào)用者main函數(shù)提供的。等GetObject()調(diào)用結(jié)束,會調(diào)用賦值操作符的重載函數(shù),將臨時的對象賦值給m1。

3.2 需要構(gòu)造臨時對象的例子--例2

用圖將該情況畫出來,如下圖所示:

tmp1和tmp2都是處于函數(shù)GetObject()開辟的棧幀上面,第一步是將需要返回的對象,通過拷貝構(gòu)造函數(shù)構(gòu)造到函數(shù)GetObject()的調(diào)用者提供的存放返回值的空間上去,即(ret)的位置。第二步是調(diào)用賦值將該臨時賦值給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)造一個m1,m1的地址為:000000CC1035FA54;然后進(jìn)入到函數(shù)GetObject(),該函數(shù)中會構(gòu)造兩個對象tmp1和tmp2,tmp1的地址為:000000CC1035F8F4,tmp2的地址為:000000CC1035F914。根據(jù)輸入決定哪個對象返回作為結(jié)果,然后將該結(jié)果賦值給m1。之后,調(diào)用了拷貝構(gòu)造函數(shù),這里出現(xiàn)了地址:000000CC1035FB34,該地址對應(yīng)的就是臨時對象,這個臨時對象的地址是由函數(shù)GetObject()的調(diào)用者main函數(shù)所提供的,這個地址位于main函數(shù)開辟的棧幀上,可以看到該地址和m1的地址離的比較近。

根據(jù)輸入的情況,我們把tmp1和tmp2中的1個通過拷貝構(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ù)的中心思想是:一個函數(shù)若直接返回類對象,一般會生成臨時類對象變量,需多次調(diào)用拷貝構(gòu)造函數(shù)(copy constructor)造成效率低下,編譯器對此優(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ā)生的地址,以及提供一個打印函數(shù),來打印某一個m1對象的地址。

通過GetObject()函數(shù)構(gòu)造一個m1對象,并打印m1的地址。通過程序運(yùn)行的結(jié)果可以發(fā)現(xiàn),當(dāng)我們進(jìn)入到GetObject()函數(shù)時,首先構(gòu)造了一個TempObj的對象tmp,我們會發(fā)現(xiàn)這里tmp對象所使用的地址就是最后對象m1的地址。也就是說在這整個過程里面,我們并沒有構(gòu)造一個另外的tmp對象,而是直接使用的是需要返回的m1的地址。整個過程中,并沒有調(diào)用拷貝構(gòu)造函數(shù)。這種優(yōu)化就叫Copy elision。

這種情況是直接使用函數(shù)的返回值去構(gòu)造一個新的對象的情況,這種情況下是不需要臨時對象的,因?yàn)橛行碌膶ο蟮牡刂罚覀兛梢灾苯邮褂眠@個地址來存放這個函數(shù)所需要返回的對象。

這種優(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)生臨時對象的情況及其解決方案的詳細(xì)內(nèi)容,更多關(guān)于C++產(chǎn)生臨時對象的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

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

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

    fork是一個在Linux系統(tǒng)環(huán)境下專有的函數(shù),現(xiàn)有的進(jìn)程調(diào)用fork后將會創(chuàng)建一個新的進(jìn)程,這里我們就來看一下Linux中使用C語言的fork()函數(shù)創(chuàng)建子進(jìn)程的實(shí)例教程
    2016-06-06
  • 最新評論