浅拷贝(系统默认):单纯的值传递;即两个类对象,完全一样,值,堆空间等。所以如果释放两者之中的一个堆空间,那么另一个的堆空间也被释放。因为他们的堆空间·是同一空间。
深拷贝(自己定义):会复制拷贝对象的所有值,当拷贝对象有自己的堆空间时,深拷贝会为自己单独申请对空间;
如图:
浅拷贝的指针指向的时同意空间
深拷贝会 有自己的堆空间
#include <iostream>
#include <string.h>
using namespace std;
struct people
{
char *name;
int age;
};
class base
{
public:
// 构造函数
base(int v, const char *name, int age)
{
this->value = new int(v);
this->p = new struct people;
this->p->name = new char[1024];
this->p->age = age;
strcpy(this->p->name, name);
cout << "构造完毕" << endl;
cout << *value << endl;
cout << this->p << endl;
cout << this->p->name << endl;
cout << (void *)this->p->name << endl;
cout << this->p->age << endl;
}
// 析构函数
~base()
{
delete value;
delete [] p->name;
delete p;
}
// 拷贝构造
base(const base &other)
{
this->value = new int(*other.value);
this->p = new struct people;
this->p->name = new char[1024];
this->p->age = other.p->age;
strcpy(this->p->name, other.p->name);
cout << "拷贝完毕" << endl;
cout << *value << endl;
cout << this->p << endl;
cout << this->p->name << endl;
cout << (void *)this->p->name << endl;
cout << this->p->age << endl;
}
private:
int *value;
struct people *p;
};
int main(int argc, char const *argv[])
{
base b(10, "zhangsan", 10);
base b2(b);
return 0;
}