1.浅拷贝&深拷贝
2.两种string类的写法
2.1传统版写法
class string
{
public:
string(const char* str = "")
{
// 构造string类对象时,如果传递nullptr指针,认为程序非法,此处断言下
if(nullptr == str)
{
assert(false);
return;
}
_str = new char[strlen(str) + 1];
strcpy(_str, str);
}
string(const string& s)
: _str(new char[strlen(s._str)+1])
{
strcpy(_str, s._str);
}
string& operator=(const string& s)
{
if(this != &s)
{
char* pStr = new char[strlen(s._str) + 1];
strcpy(pStr, s._str);
delete[] _str;
_str = pStr;
}
return *this;
}
~string()
{
if(_str)
{
delete[] _str;
_str = nullptr;
}
}
private:
char* _str;
};
2.2现代版写法
class string
{
public:
string(const char* str = "")
{
if(nullptr == str)
str = "";
_str = new char[strlen(str) + 1];
strcpy(_str, str);
}
string(const string& s)
: _str(nullptr)
{
string strTmp(s._str);
swap(_str, strTmp);
}
//这种写法更好,提高了代码复用
string& operator=(string s)
{
swap(_str, s._str);
return *this;
}
/*
string& operator=(const string& s)
{
if(this != &s)
{
string strTmp(s);
swap(_str, strTmp._str);
}
return *this;
}
*/
~string()
{
if(_str)
{
delete[] _str;
_str = nullptr;
}
}
private:
char* _str;
};
3.写时拷贝
3.1概念
- 写时拷贝可以理解为一种拖延症患者,是在浅拷贝的基础上增加了引用计数的方式来实现,那么何为引用计数?
- 引用计数:用来记录资源使用者的个数。在构造时,将资源的计数给成1,每增加一个对象使用该资源,就给计数增加1,当某个对象被销毁时,先给该计数减1,然后再检查是否需要释放资源,如果计数为1,说明该对象时资源的最后一个使用者,将该资源释放;否则就不能释放,因为还有其他对象在使用该资源
3.2两种写法
-
第一种写法
-
缺点:每构造一个新类,就会多开四个字节,会导致空间中有许多的内存碎片
class String
{
public:
String(char* str = "")
:_str(new char[strlen(str)]+1)
, _refCount(new int(1))
{
strcpy(_str, str);
}
String(const String& str)
: _str(str._str)
,_refCount(str._refCount)
{
(*_refCount)++;
}
~String()
{
release();
}
String& operator= (const String& s)
{
if (_str != s._str)
{
release();
_refCount = s._refCount;
(*_refCount)++;
_str = s._str;
}
return *this;
}
void release()
{
if ((*--_refCount) == 0)
{
delete[] _str;
delete _refCount;
}
}
private:
char* _str;
int* _refCount;
};
- 第二种写法
- ps:由于计数器存放在了
_str首地址-4
的地址上,所以在析构时一定要注意全部释放
,避免内存泄漏
class String
{
public:
String(char* str = "")
:_str(new char[strlen(str)+1+4])
{
*(int*)_str = 1;
_str += 4;
strcpy(_str, str);
}
String(const String& s)
:_str(s._str)
{
++GetCount();
}
~String()
{
release();
}
String& operator=(const String& s)
{
if (this != &s)
{
realease();
_str = s._str;
GetCount()++;
}
return *this;
}
void release()
{
if (--GetCount() == 0)
{
_str -= 4;
delete[] _str;
}
}
int& GetCount()
{
return *((int*)_str - 1);
}
private:
char* _str;
};