对象的构造,也可以由复制构造函数完成,即用一个对象的内容去初始化另一个对象的内容。此时,若对象使用了堆空间(注意和“堆对象”区分),就有深、浅复制的问题,不清楚则很容易出错。
什么是浅复制
缺省复制构造函数:用一个对象的内容初始化另一个同类对象,也称为缺省的按成员拷贝,不是对整个类对象的按位拷贝。这种复制称为浅复制。
class CGoods
{
char *Name;
int Amount;
float Price;
float Total_value;
public:
CGoods()
{
Name=new char[21];
}
CGoods(CGoods & other)
{
this->Name=other.Name;
this->Amount=other.Amount;
this->Price=other.Price;
this->Total_value=other.Total_value;
}
~CGoods()
{
delete Name;
}
};
浅复制可能带来什么问题
void main()
{
CGoods pc;
CGoods pc1(pc);
}
出现错误的原因:析构时,如果用缺省析构,则动态分配的堆空间不能回收。如果用有“delete Name;”语句的析构函数,则先析构pc1时,堆空间已经释放,然后再析构pc时出现了二次释放的问题。
解决方法:重新定义复制构造函数,给每个对象独立分配一个堆字符串,称深复制。
深复制——自定义复制构造函数
CGoods(CGoods & other)
{
this->Name=new char[21];
strcpy(this->Name,other.Name);
this->Amount=other.Amount;
this->Price=other.Price;
this->Total_value=other.Total_value;
}