C++编译器至少给一个类添加4个函数:
- 默认构造函数(无参,函数体为空)
- 默认析构函数(无参,函数体为空)
- 默认拷贝构造函数,对属性进行值拷贝。
- 赋值运算符operator=,对属性进行值拷贝。
只要是值拷贝:如果类有属性指向堆区(指针类型的字段),做赋值操作时也会出现深、浅拷贝的问题:堆中数据重复释放的问题。浅拷贝问题要通过深拷贝来解决。
通常我们写一个字段比如:int m_Age;为了把数据开辟到堆中,我们将它改为:int* m_Age;
他的构造函数在初始化字段时:m_Age = new int(age)将数据new开辟到堆区。new返回的是一个地址,刚好用int*类型的指针接收管理。
class Person
{
public:
Person(int age)
{
m_Age = new int(age); // 将数据开辟到内存堆区。
}
int* m_Age;
};
写一个测试案例:
void test01()
{
Person p1(18);
cout << "p1的年龄为:" << *p1.m_Age << endl; // 属性为指针,所以需要解引用。
Person p2(20);
cout << "p2的年龄为:" << *p2.m_Age << endl; // 属性为指针,所以需要解引用。
}
以上都是之前的知识点:现在开始变形:
令p2 = p1;
void test01()
{
Person p1(18);
cout << "p1的年龄为:" << *p1.m_Age << endl; // 属性为指针,所以需要解引用。
Person p2(20);
cout << "p2的年龄为:" << *p2.m_Age << endl; // 属性为指针,所以需要解引用。
p2 = p1; // 赋值运算的操作。
cout << "p2的年龄为:" << *p2.m_Age << endl; // 属性为指针,所以需要解引用。
}
输出:
p1的年龄为:18
p2的年龄为:20
p2的年龄为:18
此时也正常输出,看起来将p1的值,完全赋值给了p2,看起来没问题。
#include<iostream>
using namespace std;
class Person
{
public:
Person(int age)
{
m_Age = new int(age); // 将数据开辟到内存堆区。
}
int* m_Age;
};
void test01()
{
Person p1(18);
cout << "p1的年龄为:" << *p1.m_Age << endl; // 属性为指针,所以需要解引用。
Person p2(20);
cout << "p2的年龄为:" << *p2.m_Age << endl; // 属性为指针,所以需要解引用。
p2 = p1; // 赋值运算的操作。
cout << "p2的年龄为:" << *p2.m_Age << endl; // 属性为指针,所以需要解引用。
}
int main()
{
test01();
system("pause");
return 0;
}
以上都看起来很安全,没问题。
有一个情况:属性age是在堆区中。
堆区的数据,是由程序员手动开辟的,也需要程序员手动释放。
释放的时机:是在析构函数中:所以我们还要写一个析构函数来将堆区的数据释放。
~Person()
{
if (m_Age!=NULL)
{
delete m_Age;
m_Age = NULL; // 防止出现野指针。
}
}
想法是基于好意的,但是结果往往事与愿违。
这个问题,我们在深浅拷贝的问题中见过:堆区内存重复释放,程序崩溃。
两个对象p1和p2,通过浅拷贝,p1将栈中地址直接赋值给了p2,p1和p2在栈中的值,保存了同一块堆中地址。按照内存栈区释放规则,后进先出,p2要释放资源,会首先将堆中的数据清理掉。此时p1再去清理同一块区域时,就会报错。
如果不写析构,这个问题,永远不会出现,但是如果写了析构,那么这个问题,必须处理掉。
解决方案:利用深拷贝解决浅拷贝带来的问题。
在堆区重新new开辟空间。使得新对象p2栈中变量记录的地址为堆中新地址。
不同于之前通过拷贝构造函数Person p2(p1);构造p2,我们这里通过赋值运算符将p1的值,完全赋值给p2。因此我们可以考虑通过重载赋值运算符来完成深拷贝。
考虑如何重载赋值运算符。函数名肯定为operator=,返回值未知。然后赋值操作为p2=p1:相当于p2调用赋值运算符,然后传入参数为p1。因此这个函数的框架为:
void operator=(Person p)
{
}
函数体的任务是将p的属性值,全部赋值给调用这个函数。而对于指针成员变量需要在内存中开辟新空间。
因此:
void operator=(Person& p)
{
int* temp = new int(*p.m_Age);
this->m_Age = temp;
}
但是在深拷贝前,需要先判断当前的指针是否为空,先清空,再赋值。增强代码的健壮性。
void operator=(Person& p)
{
//编译器是提供浅拷贝
// m_Age = p.m_Age;
// 深拷贝:应该先判断是否有属性在堆区,如果有先释放干净,然后再深拷贝。
if (m_Age)
{
delete m_Age;
m_Age = NULL;
}
// 深拷贝具体操作。
int* temp = new int(*p.m_Age);
this->m_Age = temp;
}
代码可以正常跑起来了。但是还有一个问题:链式传递。
int a = 10;
int b = 20;
int c = 30;
c = b = a;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
我们自定义的效果如何呢?
报错原因,是我们的赋值运算符重载返回值为void。我们应该把调用者本身返回。所以返回值是调用者本身的引用(别名)。return *this;解引用返回对象本体。
Person& operator=(Person& p)
{
//编译器是提供浅拷贝
// m_Age = p.m_Age;
// 深拷贝:应该先判断是否有属性在堆区,如果有先释放干净,然后再深拷贝。
if (m_Age)
{
delete m_Age;
m_Age = NULL;
}
// 深拷贝具体操作。
int* temp = new int(*p.m_Age);
this->m_Age = temp;
return *this;
}
返回引用才是真正的自身。
Person& operator=(Person& p)
{
//编译器是提供浅拷贝
// m_Age = p.m_Age;
// 深拷贝:应该先判断是否有属性在堆区,如果有先释放干净,然后再深拷贝。
if (m_Age)
{
delete m_Age;
m_Age = NULL;
}
// 深拷贝具体操作。
int* temp = new int(*p.m_Age);
this->m_Age = temp;
return *this;
}
全部代码:
#include<iostream>
using namespace std;
class Person
{
public:
Person(int age)
{
m_Age = new int(age); // 将数据开辟到内存堆区。
}
~Person()
{
if (m_Age!=NULL)
{
delete m_Age;
m_Age = NULL; // 防止出现野指针。
}
}
Person& operator=(Person& p)
{
//编译器是提供浅拷贝
// m_Age = p.m_Age;
// 深拷贝:应该先判断是否有属性在堆区,如果有先释放干净,然后再深拷贝。
if (m_Age)
{
delete m_Age;
m_Age = NULL;
}
// 深拷贝具体操作。
int* temp = new int(*p.m_Age);
this->m_Age = temp;
return *this;
}
int* m_Age;
};
void test01()
{
Person p1(18);
cout << "p1的年龄为:" << *p1.m_Age << endl; // 属性为指针,所以需要解引用。
Person p2(20);
cout << "p2的年龄为:" << *p2.m_Age << endl; // 属性为指针,所以需要解引用。
p2 = p1; // 赋值运算的操作。
cout << "p2的年龄为:" << *p2.m_Age << endl; // 属性为指针,所以需要解引用。
Person p3(30);
p3 = p2 = p1;
cout << "p3的年龄为:" << *p3.m_Age << endl; // 属性为指针,所以需要解引用。
}
void test02()
{
int a = 10;
int b = 20;
int c = 30;
c = b = a;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
}
int main()
{
test01();
system("pause");
return 0;
}