雷火电竞-中国电竞赛事及体育赛事平台

歡迎來到入門教程網(wǎng)!

C語言

當前位置:主頁 > 軟件編程 > C語言 >

詳解C++ 臨時量與臨時對象及程序的相關優(yōu)化

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊:

一、臨時量與臨時對象

臨時量:

  1. 內置類型生成的臨時量是常量(臨時量,寄存器帶出來)。
  2. 自定義類型生成的臨時量是變量 ,在內存中。
  3. 隱式生成生成的臨時量是常量 ,顯式生成生成的臨時量是變量 。

臨時對象:

臨時對象是系統(tǒng)臨時分配的對象,在沒主動聲明所需對象而又使用其功能時產生的

顯示對象:出現(xiàn)類型名

隱式對象:不出現(xiàn)類型名

注意: 臨時對象的生存周期只在本條語句,臨時對象一旦被引用,它的生存周期就和引用相同。

對象如何生成?

先分配內存 在調用構造函數(shù)初始化對象的成員變量  產生對象對象析構了 對象就不存在了,對象的構造和析構是相反的。

重點:對象生成的順序及調用的相關函數(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); 構造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
!

二、程序優(yōu)化

  1. 1.函數(shù)調用傳對象時,按對象引用來傳遞,會少兩個函數(shù)
  2. 2.函數(shù)返回對象的時候,應該返回一個臨時對象,不要先定義,再返回
  3. 3.調用返回對象的函數(shù)時,應該以初始化的方式調用,不要以賦值的方式調用
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函數(shù)棧棧上的臨時對象
Test(const Test&)
 
// 析構tmp
~Test()
 
// 析構形參 t
~Test()
 
// t2 = GetTestObject(t1); 臨時對象調用賦值函數(shù)賦值給t2
operator=
 
// 析構臨時對象
~Test()
 
// 打印 ma
100
 
// 析構t2
~Test()
 
// 析構t1
~Test()

優(yōu)化1:函數(shù)調用傳對象時,按對象引用來傳遞,會少兩個函數(shù)

Test GetTestObject(Test &t)
{
	int value = t.getData();
	Test tmp(value);
	return tmp;
}

 

優(yōu)化2:函數(shù)返回對象的時候,應該返回一個臨時對象,不要先定義,再返回

Test GetTestObject(Test &t)
{
	int value = t.getData();
	/*Test tmp(value);
	return tmp;*/
	return Test(value);
}

優(yōu)化3:調用返回對象的函數(shù)時,應該以初始化的方式調用,不要以賦值的方式調用 

int main()
{
	Test t1;
	Test t2 = GetTestObject(t1);
	//t2 = GetTestObject(t1);
	cout << t2.getData() << endl;
 
 
 
	return 0;
}

以上所述是小編給大家介紹的C++ 臨時量與臨時對象及程序的相關優(yōu)化詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對我們網(wǎng)站的支持!

上一篇:rapidjson解析json代碼實例以及常見的json core dump問題

欄    目:C語言

下一篇:C++基類指針和派生類指針之間的轉換方法講解

本文標題:詳解C++ 臨時量與臨時對象及程序的相關優(yōu)化

本文地址:http://www.jygsgssxh.com/a1/Cyuyan/341.html

網(wǎng)頁制作CMS教程網(wǎng)絡編程軟件編程腳本語言數(shù)據(jù)庫服務器

如果侵犯了您的權利,請與我們聯(lián)系,我們將在24小時內進行處理、任何非本站因素導致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權所有