string类的浅拷贝,深拷贝,写实拷贝

string类的浅拷贝形式,这种情况下程序会奔溃,因为拷贝构造时共用了一块空间,但会析构两次,第一次释放后就为空

#include <iostream>
#include <string.h>

using namespace std;

class String
{
public:
	String (const char*  str = "")
	{
	     if (str == NULL)
	     {
		    _str = new char[1];
		    _str = '\0';
	      }
	     else
		{
		 _str=new char [strlen(str) +1];
			strcpy(_str, str);
			}
		}

		String (const String& s )
	{
		strcpy(_str,s._str);
	}
	 String&  String:: operator =(const String&  s)
	{
		if (_str != s._str)
		{
			strcpy(_str,s._str);
		}
		return (*this);
	}

		String::~String()
	{
		if ( _str != NULL)
		{
		   delete[] _str;
			_str = NULL;
		}
	}

	private:
		char* _str;
	};
深拷贝第一种形式
#include <iostream>
#include <string.h>
using namespace std;
//  深拷贝简单版本
class String
{
public:
	String(const char* str = "" )
	{
		if (NULL == str)
		{
			_str = new char [1];
			_str = '\0';
         }
		else {
			_str = new char[strlen(str)+1] ;
			strcpy(_str,str);
		}
	}

	String (const String& s)
		:_str(new char[strlen(s._str)+1] )
	{
		if (this != &s)
		{
			strcpy (_str, s._str) ;
		}
	}

	String& operator = (const String &s)
	{
		String temp(s);
		return temp;
	}

	~String ()
	{
		if (NULL != _str)
		{
			delete [] _str;
			cout << "~String()"<<endl;
          _str = NULL;
		}
	}
private:
	char* _str;
};

第二种形式:

#include <iostream>
#include <string.h>
class String
{
public :
	String(const char* str = "" )
	{
		if (NULL == str)
		{
			_str = new char [1];
			_str = '\0';
         }
		else 
		{
			_str = new char[strlen(str)+1] ;
			strcpy(_str,str);
		}
	}

	String (const String& s)
	:_str(NULL)
	{
		String temp(s._str);
		std::swap (_str , temp._str);
	}

	String& operator = (const String& s)
	{
		String temp(s);
		return temp;
	}

	~String ()
	{
		if (NULL != _str)
		{
			delete [] _str;
			std::cout << "~String()"<<std::endl;
		}
	}


private:
	char *_str;
};

上面两种形式不会出现问题,但是相同的字符串会开辟两个空间,有一定的浪费

写实拷贝引用计数int *:这种情况会造成string类的长度不一致,造成内存碎片

#include <iostream>
#include <string.h>
using namespace std;
class String 
{
public :
      String (const char* str = "")
		  : _refcount (new int [1])
	  {
         if ( NULL == str)
		 {
			 _str = new char [1];
			 _str = '\0';
		 }
		 else 
		 {
			 _str  = new char [strlen (str)+1];
			 strcpy (_str, str);
		    (*_refcount) = 1;
		}
	  }

	  String (const String&s)
		  :_str(s._str)
		  ,_refcount(s._refcount)
	  {
		  (*_refcount) ++;
	  }
        
	  String& operator = (const String& s )
	  {

		  if (this != &s)
		  {
	         String temp(*this);
		     _str = s._str;
		     _refcount = s._refcount;
		     (*_refcount) ++;
			 if( -- (*temp._refcount) == 0)
			 {
			 delete [] temp._str;
			 delete [] temp._refcount;
			 }
			 (*_refcount ) ++;
		  }
		  return (*this);
	  }

	  ~String ()
	  {
		  if ( --(*_refcount) == 0 && _str!= NULL)
		  {
			  delete [] _str;
			  delete []_refcount;
			  _str = NULL;
			  cout<<"~String ()"<< endl;
		  }
	  }
private:
	  char *_str;
      int*  _refcount;
};


写实拷贝:

#include <iostream>
#include <string.h>
using namespace std;
class String 
{
public :
	String (const char* str = "")
		:_str(new char [strlen(str)+5])
	{
		if (NULL == str)
		{
			_str = new char(5);
			*(int*)_str= 1;
			_str += 4;
			_str = '\0';
		}
		else 
		{
		     *(int*)_str= 1;
		     _str += 4;
			 strcpy (_str, str);
		}		 
	}

	String (const String& s)
		:_str(s._str)
	{
		(*(int*)(_str-4)) ++;
	}

	String operator = (const String& s)
	{
		if (_str != s._str)
		{
            Release();
			_str = s._str;
			(*(int*)(_str-4)) ++;
		}
		return (*this);
	}


	~String ()
	{
		if ( -- (*(int*)(_str-4)) == 0 )
		{
			delete [] (_str-4 );
			_str = NULL;
			cout <<"~String ()"<<endl;
		}
	}


	void Release ()
	{
		if ( --(*(int*)(_str-4)) == 0)
		{
			delete [](_str-4);
		}
	}

	friend ostream& operator<<(ostream& cout, String& s)
	{
		cout << s._str << endl;
		return cout;
	}

	char operator[] (const int index)
	{
		return _str[index];
	}

private:
	char* _str;
};


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值