国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

詳解C++ 臨時(shí)量與臨時(shí)對(duì)象及程序的相關(guān)優(yōu)化

2020-01-26 13:28:38
字體:
供稿:網(wǎng)友

一、臨時(shí)量與臨時(shí)對(duì)象

臨時(shí)量:

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

臨時(shí)對(duì)象:

臨時(shí)對(duì)象是系統(tǒng)臨時(shí)分配的對(duì)象,在沒主動(dòng)聲明所需對(duì)象而又使用其功能時(shí)產(chǎn)生的

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

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

注意: 臨時(shí)對(duì)象的生存周期只在本條語句,臨時(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)造t1Test(int) // Test t5(10,10); 構(gòu)造t5Test(int) // Test t2(20 ,20); 構(gòu)造t2Test(const Test &) // Test t3 = t2; t2拷貝構(gòu)造t3Test(int)	// static Test t4 = Test(30,30); 構(gòu)造t4Test(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 , 5Test& 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)造 TestTest(int)	// Test *p2 = new Test[2]; 構(gòu)造 TestTest(int)	// Test *p2 = new Test[2]; 構(gòu)造 TestTest(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. 1.函數(shù)調(diào)用傳對(duì)象時(shí),按對(duì)象引用來傳遞,會(huì)少兩個(gè)函數(shù)
  2. 2.函數(shù)返回對(duì)象的時(shí)候,應(yīng)該返回一個(gè)臨時(shí)對(duì)象,不要先定義,再返回
  3. 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)造t1Test(int)  // 構(gòu)造t2Test(int)  // GetTestObject(t1)實(shí)參t1通過值傳遞給Test GetTestObject(Test t) 形參 t ,調(diào)用拷貝構(gòu)造Test(const Test &)  //Test tmp(value); 構(gòu)造對(duì)象tmpTest(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ù)賦值給t2operator= // 析構(gòu)臨時(shí)對(duì)象~Test() // 打印 ma100 // 析構(gòu)t2~Test() // 析構(gòu)t1~Test()

優(yōu)化1:函數(shù)調(diào)用傳對(duì)象時(shí),按對(duì)象引用來傳遞,會(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ì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)武林網(wǎng)網(wǎng)站的支持!

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 长丰县| 鹿泉市| 康乐县| 高唐县| 买车| 株洲县| 庆安县| 柘城县| 土默特右旗| 镇赉县| 宣化县| 罗甸县| 前郭尔| 大同县| 商南县| 阿克陶县| 湘潭市| 偃师市| 通州市| 枣强县| 英吉沙县| 沐川县| 松阳县| 常山县| 什邡市| 宁波市| 沐川县| 襄城县| 高雄市| 巴青县| 松溪县| 安岳县| 鄂尔多斯市| 松滋市| 宁德市| 新泰市| 伊宁市| 和平区| 清河县| 偃师市| 远安县|