#include <iostream>
using namespace std;
class String
{
public:
String(char* str = "")
:_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(const String& s)
// :_str(NULL)
//{
// String tmp(s._str);
// swap(_str, tmp._str);
//}
//****************赋值运算符重载**************
//String& operator=(const String& s)
//{
// if (this != &s)
// {
// delete[] _str;
// _str = new char[strlen(s._str) + 1];
// strcpy(_str, s._str);
// }
// return *this;
//}
//****************赋值运算符重载**************
String& operator=(String& s)
{
swap(_str, s._str);
return *this;
}
//***************析构函数********************
~String()
{
if (_str)
{
delete[] _str;
}
}
private:
char* _str;
};
#include <iostream>
using namespace std;
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;
};
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;
};
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有