博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
String类的浅拷贝和深拷贝
阅读量:4031 次
发布时间:2019-05-24

本文共 1951 字,大约阅读时间需要 6 分钟。

拷贝有两种:深拷贝,浅拷贝

    当出现类的等号赋值时,会调用拷贝函数。在未定义显示拷贝构造函数的情况下,系统会调用默认某种拷贝函数,由系统决定是浅拷贝还是深拷贝。浅拷贝能够完成成员的复制。当数据成员中没有指针时,浅拷贝是可行的。当数据成员中有指针时,如果采用简单的浅拷贝,则两类中的两个指针将指向同一个地址,当对象快结束时,会调用两次析构函数,而导致指针悬挂现象。在这种情况下可以通过引用计数的浅拷贝来实现。

下面代码为String类的简单浅拷贝,深拷贝的传统写法和现代写法。

#include
using namespace std;class String{public: String(char* str = "")//对于string s;用""表示空 :_str(new char[strlen(str) + 1]) { strcpy(_str, str); }    //----------------------------------------------- //简单的赋值浅拷贝 //String(const String& s) // :_str(s._str) //{} //赋值运算符重载 //String& operator=(const String& s) //{ // if (_str != s._str) // { // strcpy(_str, s._str); // } // return *this; //} //深拷贝 //----------------------------------------------- //深拷贝的传统写法 //String(const String& s) // :_str(new char[strlen(s._str) + 1]) //{ // strcpy(_str, s._str); //} 赋值运算符重载 //String& operator=(const String& s) //{ // if (_str != s._str) // {//先释放后指向,否则_str无人管理会内存泄漏 // //delete[] _str;//_str占内存较小1 // //_str = new char[strlen(s._str) + 1];//s._str占内存较大1000000,会失败 // //strcpy(_str,s._str); // char* tmp = new char[strlen(s._str) + 1]; // strcpy(tmp, s._str); // delete[] _str; // _str = tmp; // } // return *this; //} //----------------------------------------------- //深拷贝的现代写法 String (const String& s) : _str(NULL)//防止释放有随机值的空间出错 { String tmp = s._str; swap(tmp._str, _str); } //赋值运算符重载 //String& operator=(const String& s) //{ //  if (_str != s._str) //  { //  String tmp(s._str); //     swap(tmp._str,_str); //  } //  return *this; //} String& operator=(String s) { if (_str != s._str) { swap(_str,s._str); } return *this; } //同一空间被多次释放 ~String() { if (_str) { cout << "~String()" << endl;     delete[] _str; } } void put() { cout << _str << endl; }private: char* _str;};void Test(){ String s; String s1(""); String s2(s1); String s3 = s1; s.put(); s1.put(); s2.put(); s3.put();}int main(){ Test(); system("pause"); return 0;}

对于引用计数的浅拷贝见本人博客:

【String类的引用计数的浅拷贝】

本文出自 “” 博客,请务必保留此出处

转载地址:http://anlbi.baihongyu.com/

你可能感兴趣的文章
python:如何将excel文件转化成CSV格式
查看>>
Django 的Error: [Errno 10013]错误
查看>>
机器学习实战之决策树(一)
查看>>
[LeetCode By Python] 2 Add Two Number
查看>>
python 中的 if __name__=='__main__' 作用
查看>>
机器学习实战之决策树二
查看>>
[LeetCode By Python]7 Reverse Integer
查看>>
[LeetCode By Python]9. Palindrome Number
查看>>
[leetCode By Python] 14. Longest Common Prefix
查看>>
[LeetCode By Python]107. Binary Tree Level Order Traversal II
查看>>
[LeetCode By Python]108. Convert Sorted Array to Binary Search Tree
查看>>
[leetCode By Python]111. Minimum Depth of Binary Tree
查看>>
[LeetCode By Python]118. Pascal's Triangle
查看>>
[LeetCode By Python]121. Best Time to Buy and Sell Stock
查看>>
[LeetCode By Python]122. Best Time to Buy and Sell Stock II
查看>>
[LeetCode By Python]125. Valid Palindrome
查看>>
[LeetCode By Python]136. Single Number
查看>>
[LeetCode By Python]167. Two Sum II - Input array is sorted
查看>>
[LeetCode BY Python]169. Majority Element
查看>>
[LeetCode By Python]172. Factorial Trailing Zeroes
查看>>