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

2025-05-27 0 57

详解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;

};

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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 详解C++中String类模拟实现以及深拷贝浅拷贝 https://www.kuaiidc.com/73190.html

相关文章

发表评论
暂无评论