C++ 对象的生存期

对象(包括简单变量)都有诞生和消失的时刻。对象诞生到结束的这段时间就是它的生存期。在生存期内,对象将保持它的状态(即数据成员的值),变量也将保持它的值不变,直到它们被更新为止。对象的生存期可以分为静态生存期动态生存期

1.局部对象

(1)对于局部定义的对象,每当程序控制流到达该对象定义处时,定义构造函数。当程序走出该局部域时,调用析构函数。         这种普通的局部对象具有动态生存期。

#include<iostream>
using namespace std;

class Complex
{
	double Real;//实部
	double Image;//虚部
public:
	Complex() :Real{}, Image{}//缺省的构造函数
	{
		cout << "构造一个对象:" << this << endl;
	}
	Complex(double r, double i) :Real{ r }, Image{ i }//带参数的构造函数
	{
		cout << "构造一个对象:" << this << endl;
	}
	~Complex()
	{
		cout << "析构一个对象:" << this << endl;
	}
	void Print() const  //常方法
	{
		cout << "Real=" << Real << '\t' << "Image" << Image << endl;
	}
};
void fun()
{
	Complex c1(1.0, 2.0);
}

void main()
{
	int n = 5;
	for(int i=0;i<n;i++)
	{
		fun();
	}
}

 运行结果:

从结果可以看出,每调用一次fun函数时,就先调用构造函数,再调用析构函数,因此会出现下面的运行结果。每次调用fun函数时,都是在给Complex类的对象c1赋值时调用构造函数,在fun函数结束时再调用析构函数,一共调用5次fun函数。
另外在每次调用fun函数时,this指针都指向Complex类的对象c1的地址。


(2)对于静态局部定义的对象,在程序控制首次到达该对象的定义处时,调用构造函数。当整个程序结束时调用析构函数。

class Complex
{
	int real;//实部
	int image;//虚部
public:
	Complex(int r = 0, int i = 0) :real(r), image(i)
	{

		cout << "Create Complex: " << this << endl;
		Print();
	}
	~Complex()
	{
		cout << "Destroy Complex: " << this << endl;
	}
	void Print() const //常方法
	{
		cout << "Real= " << real << 't' << "Image= " << image << endl;
	}
};

void fun()
{
	Complex c1(1,2);
	static Complex sc(4,5);  //.data  
	c1.Print();
	sc.Print(); 
}


int main()
{

	for (int i = 0; i < 5; i++)
	{
		fun();
	}
	return 0;

}

运行结果: 

从运行结果可以看出,因为在定义对象时有static关键字,所以该对象具有静态生存期,也就是说该对象为静态对象,当程序第一次到达该对象定义处时,调用构造函数,当这个程序结束时,也就是调用5次fun函数结束后,再调用析构函数。
注意:因为有关键字static,所以并不是调用一次fun函数,就调用一次构造函数和析构函数

2.全局对象

对于全局定义的对象,每当程序进入入口函数main之前对象就以及定义,这时要调用构造函数。整个程序结束时才调用析构函数。

全局对象具有静态生存期

//全局对象
class Complex
{
	int real;//实部
	int image;//虚部
public:
	Complex(int r = 0, int i = 0) :real(r), image(i)
	{

		cout << "Create Complex: " << this << endl;
		Print();
	}
	~Complex()
	{
		cout << "Destroy Complex: " << this << endl;
	}
	void Print() const //常方法
	{
		cout << "Real= " << real << 't' << "Image= " << image << endl;
	}
};

Complex c1(1, 1);

int main()
{
	cout << "begin main: " << endl;
	Complex tmp(2, 2);
	cout << "end main: " << endl;
	return 0;
}

Complex c3(3, 3);

运行结果如下图所示: 

3.块内对象

class Complex
{
	int real;//实部
	int image;//虚部
public:
	Complex(int r = 0, int i = 0) :real(r), image(i)
	{

		cout << "Create Complex: " << this << endl;
		Print();
	}
	~Complex()               //没有形参         把空间还给系统
	{
		cout << "Destroy Complex: " << this << endl;
	}
	void Print() const       //常方法
	{
		cout << "Real= " << real << 't' << "Image= " << image << endl;
	}
};

int main()
{
	cout << "begin main: " << endl;  //主程序开始
	Complex c1(1, 1);
	cout << "begin kuai: " << endl;
	{
		
		Complex c2(2, 2);
		
	}
	cout << "end kuai: " << endl;
	cout << "end main: " << endl;
	return 0;
}

 运行结果:

4.动态创建的对象 

使用new创建对象,delete释放对象。
(1)使用malloc和free

malloc在使用时,申请内存空间是从堆中获取。

class Complex
{
	int real;//实部
	int image;//虚部
public:
	Complex() :real(0), image(0)
	{
		cout << "Create Complex: " << this << endl;
	}
	Complex(int r, int i) :real(r), image(i)
	{
		cout << "Create Complex(int,int): " << this << endl;
	}
	~Complex()
	{
		cout << "Destroy Complex: " << this << endl;
	}
	void Print() const //常方法
	{
		cout << "Real= " << real << "Image= " << image << endl;
	}
};

//动态创建对象


//c malloc  free
//C+= new delete


int main()
{
	Complex* xp = (Complex*)malloc(sizeof(Complex)); // 1 申请空间    (只有空间没有对象)
	new(xp) Complex(2, 3);                           //通过定位new来创建空间
	//malloc 和 定位new的组合 相当于new

	xp->Print();

	xp->~Complex();
	free(xp);
	//相当于delete

	return 0;
}

运行结果:

使用new申请空间创建对象时调用构造函数,使用delete释放空间时调用析构函数。
在用new创建对象时,this指针指向该对象的地址。 

new示例1:

//动态创建对象
class Complex
{
	int real;//实部
	int image;//虚部
public:
	Complex() :real(0), image(0)
	{
		cout << "Create Complex: " << this << endl;
	}
	Complex(int r, int i) :real(r), image(i)
	{
		cout << "Create Complex(int,int): " << this << endl;
	}
	~Complex()
	{
		cout << "Destroy Complex: " << this << endl;
	}
	void Print() const //常方法
	{
		cout << "Real= " << real << "Image= " << image << endl;
	}
};

//动态创建对象


//c malloc  free
//C+= new delete


int main()
{
	int n = 5;
	Complex* xp = new Complex[n];  //1.计算大小 申请空间   2 调动构造函数 对每一个空间构造对象

	for (int i = 0; i < n; ++i)
	{
		xp[i].Print();
	}
	delete[]xp;                 //申请一组对象
	xp = nullptr;
	return 0;
}

运行结果: 

new示例2: 

//动态创建对象
class Complex
{
	int real;//实部
	int image;//虚部
public:
	Complex() :real(0), image(0)
	{
		cout << "Create Complex: " << this << endl;
	}
	Complex(int r, int i) :real(r), image(i)
	{
		cout << "Create Complex(int,int): " << this << endl;
	}
	~Complex()
	{
		cout << "Destroy Complex: " << this << endl;
	}
	void Print() const //常方法
	{
		cout << "Real= " << real << "Image= " << image << endl;
	}
};

//动态创建对象


//c malloc  free
//C+= new delete


int main()
{
	int* ip = new int(10);              //1申请空间 2 初始化
	Complex* xp = new Complex(1, 2);    //1申请空间 2 调动构造函数创建对象

	xp->Print();
	(*xp).Print();

	delete xp;     //1 调动析构函数 2 换空间
	xp = nullptr;
	delete ip;
	ip = nullptr;

	return 0;
}

运行结果:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值