摘要:本文主要向大家介绍了C++语言简单实现String类的深拷贝的普通版本和简洁版本,通过具体的代码向大家展示,希望对大家学习C++语言有所帮助。
本文主要向大家介绍了C++语言简单实现String类的深拷贝的普通版本和简洁版本,通过具体的代码向大家展示,希望对大家学习C++语言有所帮助。
String 深拷贝普通版本
[cpp] view plain copy print? 1. #define _CRT_SECURE_NO_WARNINGS // 去除 vs下 函数不安全的警告 2. #ifndef _STRING_H_ 3. #define _STRING_H_ 4. 5. #include <iostream> 6. using namespace std; 7. 8. // 深拷贝的普通版本 9. 10. class String 11. { 12. private: 13. char* _data; 14. 15. public: 16. // 构造函数 // 请注意这里只可以再定义或者声明的时候写默认值 17. String(const char* data = '\0' ); 18. String(const String& s); // 拷贝构造 19. String& operator=(const String& s); // 赋值运算符重载 20. friend ostream& operator<<(ostream& _cout, const String& s); // 输出运算符重载 21. ~String(); // 析构函数 22. 23. }; 24. 25. #endif
[cpp] view plain copy print? 1. #include "String.h" 2. 3. // String 深拷贝 普通版本 4. String::String(const char* data) 5. { 6. if (NULL == data) 7. { 8. _data = new char[1]; // 这里new [] 是为了匹配释放delete [] 9. *_data = '\0'; 10. } 11. else 12. { 13. _data = new char[strlen(data) + 1]; 14. strcpy(_data, data); 15. } 16. } 17. 18. 19. String::String(const String& s) 20. :_data(new char[strlen(s._data) + 1]) 21. { 22. strcpy(_data, s._data); 23. } 24. 25. String& String::operator=(const String& s) 26. { 27. if (this != &s) 28. { 29. // 开辟一段临时空间 防止new空间失败 丢失原来的数据 30. char* pStr = new char[strlen(s._data) + 1]; 31. strcpy(pStr, s._data); // 将等号右边的内容拷贝到新开辟的空间 32. delete[] _data; // 释放原来的空间 33. _data = pStr; //指向那段空间 34. 35. } 36. return *this; 37. } 38. 39. ostream& operator<<(ostream& _cout, const String& s) 40. { 41. _cout << s._data; 42. return _cout; 43. } 44. 45. 46. String::~String() 47. { 48. if (NULL != _data) 49. { 50. delete[] _data; 51. _data = NULL; 52. } 53. }; 54. 55. 56. int main() 57. { 58. String s1("hello"); 59. String s2(s1); 60. cout << s1 << endl; 61. cout << s2 << endl; 62. cout <<"------------------"<< endl; 63. 64. String s3("world"); 65. s3 = s1; 66. cout << s3 << endl; 67. cout <<"------------------"<< endl; 68. 69. return 0; 70. }
String 深拷贝简洁版本
头文件与前面的一样
[cpp] view plain copy print? 1. // 构造函数 2. String::String(const char* data) 3. { 4. if (NULL == data) 5. { 6. _data = new char[1]; 7. *_data = '\0'; 8. } 9. else 10. { 11. _data = new char[strlen(data) + 1]; 12. strcpy(_data, data); 13. } 14. } 15. 16. // 拷贝构造 17. String::String(const String& s) 18. :_data(NULL) // 注意这里要记得置空不然会出现替换位置区域 报错 19. { 20. String temp_str(s._data); 21. std::swap(_data, temp_str._data); 22. } 23. 24. // 赋值运算符重载 25. 26. String& String::operator=(const String& s) 27. { 28. if (this != &s) 29. { 30. String temp_str(s._data); 31. std::swap(_data, temp_str._data); 32. } 33. return *this; 34. } 35. 36. ostream& operator<<(ostream& _cout, const String& s) 37. { 38. _cout << s._data; 39. return _cout; 40. } 41. 42. String::~String() 43. { 44. if (NULL != _data) 45. { 46. delete[] _data; 47. _data = NULL; 48. } 49. }; 50. 51. int main() 52. { 53. String s1("hehe"); 54. cout << s1 << endl; 55. String s2; 56. s2 = s1; 57. cout << s2 << endl; 58. 59. String s3(s1); 60. cout << s3 << endl; 61. 62. return 0; 63. }
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C/C+频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号