写时拷贝

在前面博客中已经介绍过了普通的深拷贝构造函数和普通的赋值运算符重载函数,这里就不多做介绍了。

一.继普通拷贝构造函数之上的简单拷贝构造和简单赋值运算符重载函数

代码如下:

class String
{
private:
	char *arr;
public:
	String(const char *p = "")
	{
		if (NULL == p)
		{
			return;
		}
		this->arr = new char[strlen(p) + 1];//开辟空间
		strcpy(this->arr, p);
		cout << "执行构造函数" << endl;
	}
	~String()
	{
		delete[]arr;
		arr = NULL;
		cout << "执行析构函数" << endl;
	}
	String(const String &s):arr(NULL)//简化版拷贝构造函数,
	{
		String temp(s.arr);//执行构造函数~
		swap(arr,  temp.arr);
	}//退出函数时析构对象temp
	String& operator=( String s)//执行这个等号运算符重载前必须执行拷贝构造函数,构造匿名对象~
	{
		swap(arr, s.arr);
		return *this;
	}
	/*String & operator=(const String &s)
	{
		String temp(s.arr);
		swap(arr, temp.arr);
		return *this;
	}*/
	friend ostream& operator<<(ostream &out, const String &n)//左移运算符重载
	{
		out << n.arr;
		return out;
	}
};
自行测试。

接下来重点介绍写时拷贝构造函数

思路图如下

代码如下:外加了几个运算符重载

class String
{
private:
	char* _pStr;
public:
	String(char* pStr = "")
	{
		_pStr = new char[strlen(pStr) + 1 + 4];
		*(int *)_pStr = 1;
		strcpy(_pStr + 4, pStr);
	}
	String(const String& s)
	{
		this->_pStr = s._pStr;
		(*(int *)(_pStr))++;
	}
	String& operator=(String& s)
	{
		if (--(*(int *)(_pStr)) == 0)
		{
			delete[] _pStr;
			_pStr = NULL;
		}
		this->_pStr = s._pStr;
		(*(int *)(_pStr))++;
		return *this;
	}
	~String()
	{
		if (0 == --(*(int *)(_pStr)))
		{
			delete[] _pStr;
			_pStr = NULL;
		}
	}
	char& operator[](size_t index)
	{
		--(*(int *)(_pStr));
		char *p = new char[strlen(_pStr + 4) + 1 + 4];//开辟一块一样大的空间~
		cout << strlen(_pStr) + 1 + 8;
		(*(int *)(p)) = 1;
		strcpy(p + 4, _pStr + 4);
		_pStr = p;
		return this->_pStr[index + 4];
	}
	friend ostream& operator<<(ostream &out, const String &n)
	{
		out << n._pStr + 4;//指向的是开辟内存的起始位置,所以要加4
		return out;
	}
	bool operator>(String& s)
	{
		if (_pStr == s._pStr)
			return false;
		while (*(_pStr+4) != '\0'&&*(s._pStr+4) != '\0')
		{
			while((*(_pStr+4)==*(s._pStr+4)))
			{
				_pStr++;
				s._pStr++;
				if (*(_pStr + 4) == '\0'&&*(s._pStr + 4) == '\0')
					break;
			}
			if (*(_pStr + 4) > *(s._pStr + 4))
				return true;
			else
				return false;
		}
	}
	bool operator<(String& s)
	{
		if (_pStr == s._pStr)
			return false;
		while (*(_pStr + 4) != '\0'&&*(s._pStr + 4) != '\0')
		{
			while ((*(_pStr + 4) == *(s._pStr + 4)))
			{
				_pStr++;
				s._pStr++;
				if (*(_pStr + 4) == '\0'&&*(s._pStr + 4) == '\0')
					break;
			}
			if (*(_pStr + 4) < *(s._pStr + 4))
				return true;
			else
				return false;
		}
	}
	bool operator==(String& s)
	{
		if (_pStr == s._pStr)
			return false;
		while (*(_pStr + 4) != '\0'&&*(s._pStr + 4) != '\0')
		{
			while ((*(_pStr + 4) == *(s._pStr + 4)))
			{
				_pStr++;
				s._pStr++;
				if (*(_pStr + 4) == '\0'&&*(s._pStr + 4) == '\0')
					return true;
			}
			return false;
		}
	}
	bool operator!=( String& s)
	{
		if (_pStr == s._pStr)
			return false;
		while (*(_pStr + 4) != '\0'&&*(s._pStr + 4) != '\0')
		{
			while ((*(_pStr + 4) == *(s._pStr + 4)))
			{
				_pStr++;
				s._pStr++;
				if (*(_pStr + 4) == '\0'&&*(s._pStr + 4) == '\0')
					return false;
			}
			return true;
		}
	}
	bool strstr(const String& s)
	{
		if (_pStr == s._pStr)
			return true;
		char *dest=NULL;
		char *src = NULL;
		while (*(_pStr + 4) != '\0')
		{
			dest = _pStr + 4;
			src = s._pStr + 4;
			while (*dest++ == *src++)
			{
				if (*src == '\0')
				{
					return true;
				}
			}
			_pStr++;
		}
		return false;
	}
};

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值