C++运算符重载练习--模拟实现一个简单的C++ String类

模拟实现C++提供的string类的代码

提供的方法:

  • bool operator>(const String& rhs)

  • bool operator<(const String& rhs)

  • bool operator==(const String& rhs)

  • int length()

  • char c_str()* string—>char*

  • char& operator[](int index)

  • const char& operator[](int index) const

  • String operator+(const String& lhs, const String& rhs)

  • ostream& operator<<(ostream& out, const String& str)

#include <iostream>
using namespace std;

class String 
{
public:
	String(const char* str=nullptr) 
	{
		if (nullptr != str)
		{
			_pstr = new char[strlen(str) + 1];
			strcpy(_pstr, str);// 别忘了拷贝啊。。。。
		}
		else 
		{// 避免构造对象后底层的_pstr可能为nullptr的情况
			_pstr = new char[1];
			*_pstr = '\0';
		}
			 
	}
	~String() 
	{
		delete[]_pstr;
		_pstr = nullptr;
	}
	// 拷贝构造列表
	String(const String& rhs)// 需要进行深拷贝
	{
			 
		_pstr = new char[strlen(rhs._pstr) + 1];
		strcpy(_pstr, rhs._pstr);
	}

	// 赋值运算符重载函数,需要进行深拷贝
	String& operator=(const String& rhs)  
	{
		if (this == &rhs)
			return *this;

		delete[]_pstr;
		_pstr = new char[strlen(rhs._pstr) + 1];
		strcpy(_pstr, rhs._pstr);
	}

	bool operator>(const String& rhs) const 
	{
		return strcmp(_pstr, rhs._pstr)>0;
	}

	bool operator<(const String& rhs) const
	{
		return strcmp(_pstr, rhs._pstr) < 0;
	}

	bool operator==(const String& rhs) const
	{
		return strcmp(_pstr, rhs._pstr) == 0;
	}

	// 返回字符串有效长度
	int length()const 
	{
		return strlen(_pstr);
	}

	// 返回引用,支持对字符串内容进行修改
	char& operator[](/* String* this, */int index)
	{
		return _pstr[index];
	}

	// 常对象通过[]运算符重载函数返回的字符不能被修改
	const char& operator[](/* const String* this, */int index) const
	{
		return _pstr[index];
	}

	char* c_str() 
	{
		return _pstr;
	}
private:
    // string底层维护一个动态开辟的数组
	char* _pstr;
	 
	friend String operator+(const String& lhs, const String& rhs);
	friend ostream& operator<<(ostream& out, const String& str);
};

// 全局+运算符重载,对于String来说,+执行的是拼接操作
String operator+(const String& lhs, const String& rhs) 
{
	String tmp; 
	tmp = new char[strlen(lhs._pstr) + strlen(rhs._pstr) + 1];
	strcpy(tmp._pstr, lhs._pstr);
	strcat(tmp._pstr, rhs._pstr);
	return tmp;
}

// 输出运算符重载函数
ostream& operator<<(ostream& out, const String& str) 
{
	out << str._pstr;
	return out;
}

int main()
{
	String str1;

	/**/
	String str2 = "aaa"; // 调用了构造函数String(const char* ptr = nullptr)


	String str3 = "bbb";
	String str4 = str2 + str3;
	String str5 = str2 + "ccc";
	String str6 = "ddd" + str2;

	cout << "str6: " << str6 << endl;

	if (str5 > str6)
	{
		cout << str5 << ">" << str6 << endl;
	}
	else
	{
		cout << str5 << "<" << str6 << endl;
	}

	int len = str6.length();
	for (int i = 0; i < len; i++)
	{
		// str6.operator[](i)
		cout << str6[i] << " ";
	}
	cout << endl;

	// string -> char*
	char buf[1024] = { 0 };
	strcpy(buf, str6.c_str());
	cout << "buf: " << buf << endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

下酒番陪绅士

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值