#include<iostream>
using namespace std;
class Student {
public:
Student() {
cout << "S无参构造函数的调用" << endl;
}
Student(int b,int h) {
age = b;
hight = new int(h);
cout << "S有参构造函数的调用" << endl;
}
Student(const Student& s) {
age = s.age;
//hight = s.hight;这是浅拷贝
hight = new int(*s.hight);
cout << "S拷贝构造函数的调用" << endl;
}
~Student() {
if (hight != NULL) {
delete hight;
hight = NULL;
}
cout << "调用析构函数" << endl;
}
int age;
int *hight;
};
int main() {
Student s1(18, 180);
cout << "s1年龄:" << s1.age << "身高:" << *s1.hight << endl;
Student s2(s1);
cout << "s2年龄:" << s2.age << "身高:" << *s2.hight << endl;
return 0;
}
上面是源代码,下面是报错截图
之前用浅拷贝,在调用析构函数释放空间的时候也会报这个异常
但是浅拷贝是因为占用的同一块空间被释放了两次,为什么深拷贝还是报错啊