C++深浅拷贝——实现String类
2017-03-26 21:18
441 查看
浅拷贝:也称位拷贝,编译器只是直接将指针的值拷贝过来,结果多个对象共用同一块内存,当一个对象将这块内存释放掉之后,另一些对象不知道该块空间已经还给了系统,以为还有效,所以在对这段内存进行操作的时候,发生了访问违规。
当类里面有指针对象时,拷贝构造和赋值运算符重载只进行值拷贝(浅拷贝),两个对象向同一块内存,对象销毁时该空间被释放了两次,因此程序崩溃!
深拷贝:为不同的指针对象各自开辟空间。
浅拷贝实现String类:(错误版)
拷贝构造函数:
赋值运算符的重载:
运行结果:
深拷贝:(普通版)
当类里面有指针对象时,拷贝构造和赋值运算符重载只进行值拷贝(浅拷贝),两个对象向同一块内存,对象销毁时该空间被释放了两次,因此程序崩溃!
深拷贝:为不同的指针对象各自开辟空间。
浅拷贝实现String类:(错误版)
class String { public: String(const char *str = NULL); String(const String &other); String& operator = (const String &other); ~String(); private: char *m_data; }; String::String(const char *str) { if(str == NULL) { m_data = new char[1]; *m_data = '\0'; } else { int length = strlen(str); m_data = new char[length+1]; strcpy(m_data, str); } } String::String(const String &other) { strcpy(m_data, other.m_data); } String & String::operator =(const String &other) { //检查自赋值 if(this == &other) { return *this; } int length = strlen(other.m_data); m_data = new char[length+1]; strcpy(m_data, other.m_data); //返回当前对象的引用 return *this; } String::~String() { delete[] m_data; m_data = NULL; } int main() { String s; String s1 = "1111"; String s4 = "33333"; String s5 = "444444"; String s6 = "666"; String s2(s1); String s3 = "22222"; String s7; String s8("hello world!"); s1 = s3; system("pause"); return 0; }
拷贝构造函数:
赋值运算符的重载:
运行结果:
深拷贝:(普通版)
class String { public: String(const char *str = NULL); String(const String &other); String& operator = (const String &other); ~String(); private: char *m_data; }; String::String(const char *str) { if(str == NULL) { m_data = new char[1]; *m_data = '\0'; } else { int length = strlen(str); m_data = new char[length+1]; strcpy(m_data, str); } } String::String(const String &other) { int length = strlen(other.m_data); m_data = new char[length+1]; strcpy(m_data, other.m_data); } String & String::operator =(const String &other) { 检查自赋值 if(this == &other) { return *this; } 释放原有的内存资源 delete[] m_data; 分配新的内存资源,并复制内容 int length = strlen(other.m_data); m_data = new char[length+1]; strcpy(m_data, other.m_data); 返回当前对象的引用 return *this; } String::~String() { delete[] m_data; m_data = NULL; } int main() { String s; String s1 = "1111"; String s4 = "33333"; String s5 = "444444"; String s6 = "666"; String s2(s1); String s3 = "22222"; String s7; String s8("hello world!"); s1 = s3; system("pause"); return 0; }浅拷贝:(引用计数版)
//浅拷贝:引用计数版 class String { public: String(char *pStr = "") :_pCount(new int(1)) { if(NULL == *pStr) { pStr = new char[1]; *pStr = '\0'; } else { _pStr = new char[strlen(pStr)+1]; strcpy(_pStr,pStr); } } String(const String& s) :_pStr(s._pStr) ,_pCount(s._pCount) { ++(*_pCount); } String& operator = (const String& s) { if(--(*_pCount) == 0 && _pStr) { delete[] _pStr; delete _pCount; } _pStr = s._pStr; _pCount = s._pCount; ++(*_pCount); return *this; } ~String() { if(--(*_pCount) == 0) { delete[] _pStr; delete _pCount; _pStr = NULL; _pCount = NULL; } } private: char* _pStr; int* _pCount; };
相关文章推荐
- C++【拷贝构造】和【拷贝赋值】(实现自定义的string类)
- String类的实现与深浅拷贝问题
- C++深拷贝与浅拷贝(实现String类)
- c++ 深拷贝string类 简单实现
- 从string类谈C++深浅拷贝的区别和意义
- C++实现带写时拷贝功能的String类
- c++中深浅拷贝以及写时拷贝的实现示例代码
- 【String类浅拷贝的实现】C++:String类引用计数浅拷贝的两种实现
- C++中String类模拟实现以及深拷贝浅拷贝
- 【String类浅拷贝的实现】C++:String类引用计数浅拷贝的两种实现
- 以String类浅谈深浅拷贝——C++
- 请用c++ 实现stl中的string类,实现构造,拷贝构造,析构,赋值,比较,字符串相加,获取长度及子串等功能。
- String类的实现与深浅拷贝问题
- 【C++】模拟实现string类(版本二:用写时拷贝来实现)
- C++深浅拷贝的实现
- 【C++】浅析浅拷贝,深拷贝及写时拷贝(copy_on_write),模拟实现String类。
- C++模板实现动态顺序表(更深层次的深浅拷贝)与基于顺序表的简单栈的实现
- 【C++】面试题:模拟实现string类(版本一:用深度拷贝来实现 )
- C++【常见面试题】String类的实现,以及深拷贝、浅拷贝问题
- C++【常见面试题】String类的实现,以及深拷贝、浅拷贝问题