详解C++ 临时量与临时对象及程序的相关优化分享!

一、临时量与临时对象

临时量:

  1. 内置类型生成的临时量是常量(临时量,寄存器带出来)。
  2. 自定义类型生成的临时量是变量 ,在内存中。
  3. 隐式生成生成的临时量是常量 ,显式生成生成的临时量是变量 。

临时对象:

临时对象是系统临时分配的对象,在没主动声明所需对象而又使用其功能时产生的

显示对象:出现类型名

隐式对象:不出现类型名

注意: 临时对象的生存周期只在本条语句,临时对象一旦被引用,它的生存周期就和引用相同。

对象如何生成?

先分配内存 在调用构造函数初始化对象的成员变量  产生对象对象析构了 对象就不存在了,对象的构造和析构是相反的。

重点:对象生成的顺序及调用的相关函数

  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);  

详解C++ 临时量与临时对象及程序的相关优化 

  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  !  

二、程序优化

  1. 1.函数调用传对象时,按对象引用来传递,会少两个函数
  2. 2.函数返回对象的时候,应该返回一个临时对象,不要先定义,再返回
  3. 3.调用返回对象的函数时,应该以初始化的方式调用,不要以赋值的方式调用
  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;  }  

详解C++ 临时量与临时对象及程序的相关优化

程序分析 

  // 构造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函数栈栈上的临时对象  Test(const Test&)     // 析构tmp  ~Test()     // 析构形参 t  ~Test()     // t2 = GetTestObject(t1); 临时对象调用赋值函数赋值给t2  operator=     // 析构临时对象  ~Test()     // 打印 ma  100     // 析构t2  ~Test()     // 析构t1  ~Test()  

优化1:函数调用传对象时,按对象引用来传递,会少两个函数

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

 详解C++ 临时量与临时对象及程序的相关优化

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/483003.html

(0)
上一篇 2020年11月9日
下一篇 2020年11月9日

精彩推荐