实现字符串类型的深拷贝
来源:互联网 发布:叮叮办公软件 编辑:程序博客网 时间:2024/06/10 04:47
#define _CRT_SECURE_NO_WARNINGS 1#include<stdio.h>#include<stdlib.h>#include<iostream>using namespace std;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) { char* ptemp = new char[strlen(s._pstr) + 1]; strcpy(s._pstr,ptemp); delete[] _pstr; _pstr = NULL; _pstr = ptemp; } } ~String() { if (NULL != _pstr) { delete[] _pstr; _pstr = NULL; } }private: char* _pstr;};void test(){ String s1("12345"); String s2(s1); String s3 = s2;}int main(){ test(); system("pause"); return 0;}
实现深拷贝主要是为了解决在浅拷贝中所遇到的一块内存空间被多个对象共同使用的情况,最后在释放对象空间的时候就会省去很多不必要的麻烦。
0 0
- 实现字符串类型的深拷贝
- c++实现字符串的深拷贝
- 字符串拷贝strcpy的实现
- JS实现clone函数对五种数据类型的复制(深拷贝、浅拷贝)
- 字符串的拼接和拷贝的实现
- 字符串拷贝函数的满分实现
- 实现自己的字符串拷贝函数Strcpy
- 实现字符串拷贝的七种方法
- 20170912_字符串拷贝strcpy的实现
- 字符串拷贝--strcpy的代码实现
- 深拷贝的实现
- 深拷贝的实现
- 自己实现字符串拷贝
- 深拷贝与浅拷贝的实现
- 浅拷贝与深拷贝、以及深拷贝的实现
- 实现深拷贝的String
- javascript深拷贝的实现
- 堆和拷贝构造函数--深拷贝与浅拷贝,用于类型转换的构造函数
- 3sum leetcode
- Excel Sheet Column Number
- 搜索插入位置
- gangliaWeb汉化及优化(二)--连接hadoop
- Retrofit的讲解和使用
- 实现字符串类型的深拷贝
- 小白自学之路---html常用标签
- JNI/NDK开发指南(二)
- leetcode 122. Best Time to Buy and Sell Stock II
- 2017华为精英挑战赛总结
- div高度自适应
- 向这个崭新的世界发出的第一声啼鸣——Hello World!
- JNI/NDK开发指南(三)
- 启动模拟器 Intel HAXM is required to run this AVD VT-x is disabled in BIOS 问题