服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - 详解C++中String类模拟实现以及深拷贝浅拷贝

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

2021-06-09 14:17xy913741894 C/C++

这篇文章主要介绍了详解C++中String类模拟实现以及深拷贝浅拷贝的相关资料,希望通过本文能帮助到大家,让大家实现这样的方法,需要的朋友可以参考下

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

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

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

下面通过代码进行对比:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//浅拷贝
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;
};
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//深拷贝
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--。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//浅拷贝优化--带有计数版本的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来简化操作,代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//深拷贝的简洁写法
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;
};

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

原文链接:http://blog.csdn.net/xy913741894/article/details/52890448

延伸 · 阅读

精彩推荐