您的位置:首页 > 编程语言 > C语言/C++

C++深浅拷贝——实现String类

2017-03-26 21:18 441 查看
浅拷贝:也称位拷贝,编译器只是直接将指针的值拷贝过来,结果多个对象共用同一块内存,当一个对象将这块内存释放掉之后,另一些对象不知道该块空间已经还给了系统,以为还有效,所以在对这段内存进行操作的时候,发生了访问违规。

当类里面有指针对象时,拷贝构造和赋值运算符重载只进行值拷贝(浅拷贝),两个对象向同一块内存,对象销毁时该空间被释放了两次,因此程序崩溃!

深拷贝:为不同的指针对象各自开辟空间。

浅拷贝实现String类:(错误版)

class String
{
public:
String(const char *str = NULL);
String(const String &other);
String& operator = (const String &other);
~String();
private:
char *m_data;
};

String::String(const char *str)
{
if(str == NULL)
{
m_data = new char[1];
*m_data = '\0';
}
else
{
int length = strlen(str);
m_data = new char[length+1];
strcpy(m_data, str);
}
}

String::String(const String &other)
{
strcpy(m_data, other.m_data);
}

String & String::operator =(const String &other)
{
//检查自赋值
if(this == &other)
{
return *this;
}
int length = strlen(other.m_data);
m_data = new char[length+1];
strcpy(m_data, other.m_data);

//返回当前对象的引用
return *this;
}

String::~String()
{
delete[] m_data;
m_data = NULL;
}

int main()
{
String s;
String s1 = "1111";
String s4 = "33333";
String s5 = "444444";
String s6 = "666";
String s2(s1);
String s3 = "22222";
String s7;
String s8("hello world!");

s1 = s3;

system("pause");
return 0;
}


拷贝构造函数:



赋值运算符的重载:



运行结果:



深拷贝:(普通版)

class String
{
public:
String(const char *str = NULL);
String(const String &other);
String& operator = (const String &other);
~String();
private:
char *m_data;
};

String::String(const char *str)
{
if(str == NULL)
{
m_data = new char[1];
*m_data = '\0';
}
else
{
int length = strlen(str);
m_data = new char[length+1];
strcpy(m_data, str);
}
}

String::String(const String &other)
{
int length = strlen(other.m_data);
m_data = new char[length+1];
strcpy(m_data, other.m_data);
}

String & String::operator =(const String &other)
{
检查自赋值
if(this == &other)
{
return *this;
}

释放原有的内存资源
delete[] m_data;

分配新的内存资源,并复制内容
int length = strlen(other.m_data);
m_data = new char[length+1];
strcpy(m_data, other.m_data);

返回当前对象的引用
return *this;
}

String::~String()
{
delete[] m_data;
m_data = NULL;
}

int main()
{
String s;
String s1 = "1111";
String s4 = "33333";
String s5 = "444444";
String s6 = "666";
String s2(s1);
String s3 = "22222";
String s7;
String s8("hello world!");

s1 = s3;

system("pause");
return 0;
}
浅拷贝:(引用计数版)

//浅拷贝:引用计数版
class String
{
public:
String(char *pStr = "")
:_pCount(new int(1))
{
if(NULL == *pStr)
{
pStr = new char[1];
*pStr = '\0';
}
else
{
_pStr = new char[strlen(pStr)+1];
strcpy(_pStr,pStr);
}
}
String(const String& s)
:_pStr(s._pStr)
,_pCount(s._pCount)
{
++(*_pCount);
}
String& operator = (const String& s)
{
if(--(*_pCount) == 0 && _pStr)
{
delete[] _pStr;
delete _pCount;
}
_pStr = s._pStr;
_pCount = s._pCount;
++(*_pCount);

return *this;
}
~String()
{
if(--(*_pCount) == 0)
{
delete[] _pStr;
delete _pCount;
_pStr = NULL;
_pCount = NULL;
}
}

private:
char* _pStr;
int* _pCount;
};


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++ String类 深浅拷贝