前面的文章深拷贝中提到了浅拷贝是错误的。 但是如下的改动可以让他死而复生。
#include <iostream>
using namespace std;
class String
{
public:
String(const char* pstr = "")
:_pcount(new int)
{
if(NULL == pstr)
{
_pstr = new char[1];
*_pstr = '\0';
}
else
{
_pstr = new char[strlen(pstr)+1];
strcpy(_pstr,pstr);
}
*_pcount = 1;
}
String(const String& t)
:_pstr(t._pstr)
,_pcount(t._pcount)
{
++(*_pcount);
}
String& operator=(const String& t)
{
if(this != &t)
{
if( 0 == --(*_pcount))
{
delete[] _pstr;
delete _pcount;
}
_pstr = t._pstr;
_pcount = t._pcount;
++(*_pcount);
}
return *this;
}
~String()
{
if(0 == --(*_pcount))
{
delete[]_pstr;
delete _pcount;
_pstr = NULL;
_pcount = NULL;
}
}
private:
char* _pstr;
int* _pcount;
};
void Funtest()
{
String s1("hello");
String s2(s1);
String s3;
s3 = s2;
}
int main ()
{
Funtest();
return 0;
}