C++构造函数析构函数拷贝构造函数

1.C++构造函数和析构函数

构造函数:
构造函数在创建类对象时自动执行,通常用于一些数据的初始化工作,构造函数可以重载,有一个默认构造函数。

析构函数:
在释放对象所占内存时自动执行,不能重载,一个类只能有一个析构函数


构造函数和析构函数的执行顺序是,先构造后析构,后构造先析构,类似于栈,先进后出

#include <iostream>
#include <string>
using namespace std;
class CExample
{
public:
	CExample(string);
	~CExample();
private:
	string m_name;
};
CExample::CExample(string name)
{
	m_name=name;
	cout<<m_name<<" object  constructor Run."<<endl;
}
CExample::~CExample()
{
	cout<<m_name<<" object  destructor Run."<<endl;
}
void main()
{
	CExample a("a");
	CExample b("b");
}
如上代码输出顺序是:
a  object consreuctor Run.
b  object consreuctor Run.
b  object desreuctor Run.
a  object desreuctor Run.

这里,我们不比其他语言,需要new  。  不使用new的对象都存放在栈中。所以为什么b会先析构,a后析构

当然,我们把上面的代码改成如下:
#include <iostream>
#include <string>
#define SAFE_DELETE(p) if(p){delete(p);p=0;}
using namespace std;
class CExample
{
public:
	CExample(string);
	~CExample();
private:
	string m_name;
};
CExample::CExample(string name)
{
	m_name=name;
	cout<<m_name<<" object  constructor Run."<<endl;
}
CExample::~CExample()
{
	cout<<m_name<<" object  destructor Run."<<endl;
}
void main()
{
	CExample *a=new CExample("a");
	CExample *b=new CExample("b");
	SAFE_DELETE(a);
	SAFE_DELETE(b);
}




	
这样子就需要自己手动释放了,应为new的对象存放在堆中,它是不会自动释放的。需要我们手动进行内存释放。

但是我们在学习C#的时候,好像是局部变量在方法结束后会自动释放,按理说main方法结束后   a和b应该会自动释放内存的啊 ,我们在改改代码,来验证下C++中是不是也一样
#include <iostream>
#include <string>
#define SAFE_DELETE(p) if(p){delete(p);p=0;}
using namespace std;
class CExample
{
public:
	CExample(string);
	~CExample();
private:
	string m_name;
};
CExample::CExample(string name)
{
	m_name=name;
	cout<<m_name<<" object  constructor Run."<<endl;
}
CExample::~CExample()
{
	cout<<m_name<<" object  destructor Run."<<endl;
}
void func()
{
	CExample *a=new CExample("a");
	CExample *b=new CExample("b");
}
void main()
{
	func();
	cout<<"main function run end"<<endl;
}
可以看出结构,就算func执行结束后,也不会释放。也许这就是一点不同吧

2.拷贝构造函数

拷贝构造函数:
拷贝构造函数和对基本类型的变量赋值一样,但是拷贝的是类,类里面的结构是很复杂的,所以这里的深究一样。

using namespace std;
class CExample
{
public:
	CExample(string);
	//CExample(const CExample& c);
	~CExample();
private:
	string m_name;
};
CExample::CExample(string name)
{
	m_name=name;
	cout<<m_name<<" object  constructor Run."<<endl;
}
CExample::~CExample()
{
	cout<<m_name<<" object  destructor Run."<<endl;
}

void main()
{
	CExample a("a");
	CExample b=a;
}
这段代码,并没有构造两次,但是析构的两次,这是因为什么呢?

当执行CEXample b=a的时候,其实就调用了拷贝构造函数,如果没有显示指定一个拷贝构造函数,系统会有一个默认的,这个和构造函数一样。

我们看下面一段代码,显示指定一个拷贝构造函数

using namespace std;
class CExample
{
public:
	CExample(string);
	CExample(const CExample& c);
	~CExample();
public:
	string m_name;
};
CExample::CExample(string name)
{
	m_name=name;
	cout<<m_name<<" object  constructor Run."<<endl;
}
CExample::~CExample()
{
	cout<<m_name<<" object  destructor Run."<<endl;
}
CExample::CExample(const CExample& c)
{
	m_name=c.m_name;
	cout<<m_name<<" object copy constructor Run."<<endl;
}
void main()
{
	CExample a("a");
	CExample b=a;
}

这样纸,首先构造a   然后调用了copy constructor   最后在析构了两个对象

那么,他们两个对象会不会相互影响呢??
#include <iostream>
#include <string>
#define SAFE_DELETE(p) if(p){delete(p);p=0;}
using namespace std;
class CExample
{
public:
	CExample(string);
	CExample(const CExample& c);
	~CExample();
public:
	string m_name;
};
CExample::CExample(string name)
{
	m_name=name;
	cout<<m_name<<" object  constructor Run."<<endl;
}
CExample::~CExample()
{
	cout<<m_name<<" object  destructor Run."<<endl;
}
CExample::CExample(const CExample& c)
{
	m_name=c.m_name;
	cout<<m_name<<" object copy constructor Run."<<endl;
}
void main()
{
	CExample a("a");
	CExample b=a;
	b.m_name="updateb";
	a.m_name="updatea";
}
这里表示,他们并不会互相影响。

3.深拷贝和浅拷贝

浅拷贝:
#include <iostream>
using namespace std;
class CExample
{
public:
	CExample();
	~CExample();
private:
	int *p;
};
CExample::CExample()
{
	p=new int(100);
	cout<<" object  constructor Run."<<endl;
}
CExample::~CExample()
{
	if (p)
	{
		delete(p);
		p=0;
	}
	cout<<" object  destructor Run."<<endl;
}
void main()
{
	CExample a;
	CExample b=a;
}

这段代码运行结束后会出现一个错误,应为里面有个指针是new出来的,对象赋值的时候并没有正确的处理,所以当析构的时候同一个内存块被释放两次

如下图表示:


这时候我们应该要用深拷贝:

#include <iostream>
using namespace std;
class CExample
{
public:
	CExample();
	CExample(const CExample& c);
	~CExample();
private:
	int *p;
};
CExample::CExample()
{
	p=new int(100);
	cout<<" object  constructor Run."<<endl;
}
CExample::CExample(const CExample& c)
{
	p=new int();
	*p=*(c.p);
}
CExample::~CExample()
{
	if (p)
	{
		delete(p);
		p=0;
	}
	cout<<" object  destructor Run."<<endl;
}
void main()
{
	CExample a;
	CExample b=a;
}

解释完毕,有时候,为了防止默认浅拷贝发生,最好是私有化拷贝构造函数

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值