数据分为基本数据类型和引用数据类型
值类型(基本类型):数据直接存储在栈中,例如:字符串(string)、数值(number)、布尔值(boolean)、undefined、null
引用类型:存储在栈中的是对象的引用地址,对象数据存放在堆内存中,例如:对象(object)、数组(array)、函数(function)
seqb先释放data seqa在释放时 data已经被释放了
//浅拷贝
SeqList(const SeqList& str)
:cursize(str.cursize), maxsize(str.maxsize),data(str.data)
//缺省的拷贝构造
{ }
SeqList& operator=(const SeqList& str)
{ //缺省的等好远算符重载
if (this != &str)
{
cursize = str.cursize;
maxsize = str.maxsize;
data = str.data;
}
return *this;
}
出现内存泄漏
凡是在类中需要设计指向内核态,设计信号量,互斥量时 都必须要设计自己的拷贝构造
如果不需要拷贝构造时
SeqList (const SeqList& str) = delete;
//防止浅拷贝
if (NULL != src._name)
{ //判空
_name = new char[strlen(src._name) + 1];
memset(_name, 0, strlen(src._name) + 1);
for (int i = 0; i < strlen(src._name); i++)
{
_name[i] = src._name[i];
}
}
else
{
_name = NULL;
}
各对象的data都指向自己的堆区
//深拷贝
SeqList(const SeqList& str)
{
maxsize = str.maxcursize;
cursize = str.cursize;
data = (int*)malloc(sizeof(int)*maxsize);
//自己开辟空间
memcpy(data,str.data,sizeof(int)*str.cursize);
//有数据 导向 释放就不会出现重复释放
}
SeqList& operator=(const SeqList& str)
{
if (this != &str)
{
cursize = str.cursize;
maxsize = str.maxsize;
free(data);
data = (int*)malloc(sizeof(int)*str.maxsize);
memcpy(data,str.data,sizeof(str.cursize);
return *this;
}