对象---关于对象的生命周期考题的分析

对象---关于对象的生命周期考题的分析 

目录

对象---关于对象的生命周期考题的分析 

综上小结:

面试例题:

 小结:



#include <iostream>
using namespace std;

class Test
{
public:
	Test(int data = 100) : ma(data) 
	{
		cout << "Test(int)" << endl;
	}

	Test(const Test &src) : ma(src.ma)
	{
		cout << "Test(const Test &)" << endl;
	}

	Test & operator = (const Test &src)
	{
		ma = src.ma;
		cout << "Test& operator=(const Test &)" << endl;
		return *this;
	}

	~Test()
	{
		cout << "~Test()" << endl;
	}

	int getData() { return ma; }
private:
	int ma;
};

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

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

输出:

结构分析: 

Test(int)------t1构造
Test(int)       t2构造
Test(const Test &)    t1拷贝构造形参t
Test(int)  函数中的tmp构造
Test(const Test &) tmp拷贝构造临时对象
~Test()  tmp析构
~Test()  形参析构
Test& operator=(const Test &)   临时对象赋值给t2
~Test()   临时对象的析构
100

~Test()  t2析构
~Test()   t1析构

 


#include <iostream>
using namespace std;

class Test
{
public:
	Test(int data = 100) : ma(data) 
	{
		cout << "Test(int)" << endl;
	}

	Test(const Test &src) : ma(src.ma)
	{
		cout << "Test(const Test &)" << endl;
	}

	Test & operator = (const Test &src)
	{
		ma = src.ma;
		cout << "Test& operator=(const Test &)" << endl;
		return *this;
	}

	~Test()
	{
		cout << "~Test()" << endl;
	}

	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;
	
	Test t2 = GetTestObject(t1);//---修改三 相当于t2直接初始化了 (t2)构造
//具体原因与编译器有关 ,临时变量在应该赋值给t2的,但是编辑器直接优化为t2的构造,大大节省生成没必要的临时变量
	cout << t2.getData() << endl;
	system("pause");
	return 0;
}

执行结构:

Test(int)   t1构造
Test(int)   t2构造
100

~Test()  t2析构
~Test()   t1析构

综上小结:

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

面试例题:


#include <iostream>
using namespace std;

class Test
{
public:// Test() Test(a)  Test(a, b)
	Test(int a = 5, int b = 5) :ma(a), mb(b)
	{ cout << "Test(int)" << this<<endl; }
	~Test()
	{ cout << "~Test()" << this<<endl; }
	Test(const Test &src) :ma(src.ma), mb(src.mb)
	{ cout << "Test(const Test&)" << endl; }
	Test& operator=(const Test &src)
	{
		cout << "operator=" << endl;
		ma = src.ma;
		mb = src.mb;
		return *this;
	}
private:
	int ma;
	int mb;
};
Test t1(10, 10);    //全局对象
int main()
{
	Test t2(20, 20);
	Test t3 = t2;
	static Test t4 = Test(30, 30);  //静态局部对象在data区
	t2 = Test(40, 40);
	t2 = (Test)(50, 50);
	t2 = 60; 
	Test *p1 = new Test;    //堆区  局部对象
	Test *p2 = new Test[2];   //堆区 局部对象
	Test *p3 = &Test(70, 70);
	Test &p4 = Test(80, 80);    //局部对象 引用
	delete p1;
	delete[]p2;
	//system("pause");
	return 0;
}
Test t5(90, 90);    //全局对象

运行结果如下: 

Test(int)00330584      t1构造
Test(int)00330578      t5构造
Test(int)0027FD98     t2构造
Test(const Test&)       t2拷贝构造t3
Test(int)0033058C     t4构造
Test(int)0027FBDC    临时对象构造
operator=      临时对象拷贝t2
~Test()0027FBDC  临时对象析构
Test(int)0027FBEC   临时对象构造
operator=    临时对象拷贝t2
~Test()0027FBEC  临时对象析构
Test(int)0027FBFC   临时对象构造(隐式)
operator=   临时对象拷贝t2
~Test()0027FBFC  临时对象析构
Test(int)00707148  在堆上构造一个对象
Test(int)00706474  在堆上构造一个test[2]数组,构造两次,(第一次)
Test(int)0070647C  在堆上构造一个test[2]数组,构造两次,(第二次)
Test(int)0027FC3C  临时对象构造
~Test()0027FC3C    临时对象析构  (说明了临时对象的生命周期其实就是当前表达式执行完成后,直接析构)
Test(int)0027FD48   临时对象构造 ,但是p4引用它
~Test()00707148      delete操作释放了p1指向的new开辟的堆上的对象,对象析构
~Test()0070647C      delete操作释放了p2指向new开辟的堆上的对象数组,数组中的的第二个析构
~Test()00706474       对象数组中第一个对象析构

接下来依次是p4  t3 t2  t4  t5  t1析构

 

 小结:

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

3.全局对象先构造,接下来顺序依次构造局部,静态局部;但是在析构时,局部按构造逆序先析构,接下来是静态局部对象(直到出自己的定义的作用域才析构),再是全部对象的析构

4.delete 释放内存 ,其上的对象直接析构

5. -局部对象:当程序执行流到达对象构造语句时进行构造;堆对象:当程序执行流执行到new关键字时创建对象,new创建对象时会自动调用构造函数

6.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值