C++中String类模拟实现以及深拷贝浅拷贝

时间:2022-04-08 21:58:18

在C语言中/C++中,字符串是一个应用很广泛的类型,也是很基础的类型,C语言并没有直接处理字符串的操作而是采用字符指针和字符串数组进行操作,而在C++中标准库为我们封装了一个字符串的类供我们使用,使用需要#inlcude <string>头文件。我们也可以自己模拟实现一个简单的String类。

在模拟实现String类的过程中,不可避免的会遇到深拷贝浅拷贝的问题,下面就深拷贝浅拷贝做一个简介。所谓深拷贝浅拷贝,简单来说就是浅拷贝只是简单的将值拷贝过来,用一个对象初始化另一个对象,只复制了成员,并没有复制资源,使两个对象同时指向了同一资源的而深拷贝则是将资源和值一块拷贝过来,此时两个对象各自占用资源,尽管值相同,但是互不影响。

下面通过代码进行对比:

//浅拷贝
class String {
public:
String(const char* s = "")
{
if (NULL == s) {
_pStr = new char[1];
*_pStr = '\0';
}
else {
_pStr = new char[strlen(s) + 1];
strcpy(_pStr, s);
}
}
String(const String& s)
{
_pStr = s._pStr;
}
String& operator=(const String& s)
{
if (this != &s) {
_pStr = s._pStr;
}
return *this;
}
~String()
{
if (NULL != _pStr) {
delete[] _pStr;
_pStr = NULL;
}
}

private:
char* _pStr;
};


//深拷贝
class String {
public:
String(const char* s = "")
{
if (NULL == s) {
_pStr = new char[1];
*_pStr = '\0';
}
else {
_pStr = new char[strlen(s) + 1];
strcpy(_pStr, s);
}
}
String(const String& s) : _pStr(new char[strlen(s._pStr) + 1])
{
strcpy(_pStr, s._pStr);
}
String& operator=(const String& s)
{
if (this != &s) { //先申请空间将s的内容拷贝到一个临时变量再去释放原有的空间
char* temp = new char[strlen(s._pStr) + 1];//防止申请空间失败连原有的空间都没了
strcpy(temp, s._pStr);
delete[] _pStr;
_pStr = NULL;
_pStr = temp;
}
return *this;
}
~String()
{
if (NULL != _pStr) {
delete[] _pStr;
_pStr = NULL;
}
}
private:
char* _pStr;
};

C++中String类模拟实现以及深拷贝浅拷贝C++中String类模拟实现以及深拷贝浅拷贝

由图可以看出,浅拷贝使得多个对象指向一块空间,然而当最后析构的时候,比如c先释放空间,而a,b却不知道还要释放空间便会产生重复释放同一内存的错误。为此,我们可以对浅拷贝进行一个优化,例如在私有成员中加入一个int* pCount来标记一块空间被几个对象占用,当只有一个对象占用如果进行析构便可释放空间,否则只对*pCount--。

//浅拷贝优化--带有计数版本的String类,用指针指向计数的空间
class String {
public:
String(const char* s = "") : _pCount(new int(1))
{
if (NULL == s) {
_pStr = new char[1];
*_pStr = '\0';
}
else {
_pStr = new char[strlen(s) + 1];
strcpy(_pStr, s);
}
}
String(const String& s)
{
_pStr = s._pStr;
_pCount = s._pCount;
(*_pCount)++;
}
String& operator=(const String& s)
{
if (this != &s) {
if (--(*_pCount) == 0) {
delete[] _pStr;
delete _pCount;
}
_pStr = s._pStr;
_pCount = s._pCount;
(*_pCount)++;
}
return *this;
}
~String()
{
if (NULL != _pStr && --(*_pCount) == 0) {
delete[] _pStr;
delete _pCount;
}
_pCount = NULL;
_pStr = NULL;
}

private:
char* _pStr;
int* _pCount;
};
最后再给出一种深拷贝的简洁版本,通过调用swap来简化操作,代码如下:

//深拷贝的简洁写法
class String {
public:
String(const char* s = "")
{
if (NULL == s) {
_pStr = new char[1];
*_pStr = '\0';
}
else {
_pStr = new char[strlen(s) + 1];
strcpy(_pStr, s);
}
}
String(String& s) :_pStr(NULL)//必须对_pStr初始化,防止释放随机值的空间
{
String temp(s._pStr);
swap(_pStr, temp._pStr);
}
String& operator=(String& s)
{
if (this != &s) {
swap(_pStr, s._pStr);
}
return *this;
}
~String()
{
if (NULL != _pStr) {
delete[] _pStr;
_pStr = NULL;
}
}
private:
char* _pStr;
};