C++中的浅拷贝是产生很多问题的根本原因,其根本原因是在有指针的时候,只是拷贝了一个指针的值,多个指针指向同一块内存区域,当free内存时,造成其他指针指向的空间不存在。结合构造函数和析构函数理解浅拷贝是一个不错的选择
原始程序
#include "iostream"
using namespace std;
class Name
{
public:
Name(const char *pname)
{
size = strlen(pname);
pName = (char *)malloc(size + 1);
strcpy(pName, pname);
}
~Name()
{
cout<<"开始析构"<<endl;
if (pName!=NULL)
{
free(pName);
pName = NULL;
size = 0;
}
}
protected:
private:
char *pName;
int size;
};
void playObj()
{
Name obj1("obj1.....");
}
void main()
{
playObj();
system("pause");
}
对象的初始化
使用对象的初始化时候,会调用copy构造函数,在copy构造函数中进行深拷贝
#include "iostream"
using namespace std;
class Name
{
public:
Name(const char *pname)
{
size = strlen(pname);
pName = (char *)malloc(size + 1);
strcpy(pName, pname);
}
Name(Name &obj)
{
//用obj来初始化自己
pName = (char *)malloc(obj.size + 1);
strcpy(pName, obj.pName);
size = obj.size;
}
~Name()
{
cout<<"开始析构"<<endl;
if (pName!=NULL)
{
free(pName);
pName = NULL;
size = 0;
}
}
protected:
private:
char *pName;
int size;
};
void playObj()
{
Name obj1("obj1.....");
Name obj2 = obj1; //obj2创建并初始化
}
void main()
{
playObj();
system("pause");
}
对象的=操作
在对象的=操作的时候,也是浅拷贝,此时需要使用运算符重载来解决问题
#include "iostream"
using namespace std;
class Name
{
public:
Name(const char *pname)
{
size = strlen(pname);
pName = (char *)malloc(size + 1);
strcpy(pName, pname);
}
Name(Name &obj)
{
//用obj来初始化自己
pName = (char *)malloc(obj.size + 1);
strcpy(pName, obj.pName);
size = obj.size;
}
~Name()
{
cout<<"开始析构"<<endl;
if (pName!=NULL)
{
free(pName);
pName = NULL;
size = 0;
}
}
void operator=(Name &obj3)
{
//用obj3来=自己
pName = (char *)malloc(obj3.size + 1);
strcpy(pName, obj3.pName);
size = obj3.size;
}
protected:
private:
char *pName;
int size;
};
void playObj()
{
Name obj1("obj1.....");
Name obj2 = obj1; //obj2创建并初始化
Name obj3("obj3...");
obj2 = obj3;
}
void main()
{
playObj();
system("pause");
}
上面程序存在的问题
在以上程序中,存在内存泄漏,obj2的指针重新指向的时候,原来的内存没有释放
#include "iostream"
using namespace std;
class Name
{
public:
Name(const char *pname)
{
size = strlen(pname);
pName = (char *)malloc(size + 1);
strcpy(pName, pname);
}
Name(Name &obj)
{
//用obj来初始化自己
pName = (char *)malloc(obj.size + 1);
strcpy(pName, obj.pName);
size = obj.size;
}
~Name()
{
cout<<"开始析构"<<endl;
if (pName!=NULL)
{
free(pName);
pName = NULL;
size = 0;
}
}
void operator=(Name &obj3)
{
if (pName != NULL)
{
free(pName);
pName = NULL;
size = 0;
}
//用obj3来=自己
pName = (char *)malloc(obj3.size + 1);
strcpy(pName, obj3.pName);
size = obj3.size;
}
protected:
private:
char *pName;
int size;
};
void playObj()
{
Name obj1("obj1.....");
Name obj2 = obj1; //obj2创建并初始化
Name obj3("obj3...");
obj2 = obj3;
}
void main()
{
playObj();
system("pause");
}