【C++】运算符重载实现string类自我实现

目录

 

开发环境

注意点(不只是String类所要注意的点,所有的C++类都需要注意的地方)

实现代码

相关的测试代码


开发环境

win10+vs2017

注意点(不只是String类所要注意的点,所有的C++类都需要注意的地方)

  • 等号赋值的三步走1,防止自己给自己赋值 2,释放之前所占的外部资源 3,申请新的空间然后将数据拷贝进去
     
  • 析构函数执行后记得要把指针变量指向nullptr,避免野指针的出现。举例<如果析构没有在释放内存后将指针指向空>
    String str1;
    str.~String();
    str.~String();
    str在第二次调用析构函数后delete一个野指针程序必崩。
  • 在String类中写参数为const String&等号"="运算符函数的时候记得将返回定义为String,这么做的目的是可以实现这样的连等操作
    String str1
    String str2
    String str3
    str1=str2=str3

 

实现代码

#include<iostream>
using namespace std;

enum error_num {OUTOFRANDGE};

//1,保证string没有空(不然每次操作的时候都需要判断是否为空,很麻烦)
//2,不要出现野指针
class String
{

public:
	String(const char*ptr=nullptr)
	{
		//保证String类中_ptr不为空指针
		if (ptr != nullptr)
		{
			_ptr = new char[strlen(ptr) + 1];
			strcpy(_ptr, ptr);
		}
		else
		{
			_ptr = new char[1];
			_ptr = '\0';
		}
	}
	~String() { delete[]_ptr; _ptr = nullptr; }
	String(const String &src) 
	{
		_ptr = new char[strlen(src._ptr) + 1];
		strcpy(_ptr, src._ptr);
	}
	String(String &&src)
	{
		_ptr = src._ptr;
		src._ptr = nullptr;//++++++++++++++很重要很重要很重要很重要
	}
	//返回值为String目的是为了可以连等于操作
	String& operator=(const String &src)
	{
		if (&src == this)
			return *this;
		delete[]this->_ptr;
		_ptr = new char[strlen(src._ptr) + 1];
		strcpy(_ptr, src._ptr);
		return *this;
	}
	String operator=(String &&src)
	{
		delete[]this->_ptr;//首先释放自己占有的外部资源,不然容易引起内存泄漏
		_ptr = src._ptr;
		src._ptr = nullptr;
	}
	//等号右边的操作数为const char*不存在连等赋值,所以返回值为void
	void operator=(const char*_str)
	{
		delete[]this->_ptr;
		if (_str == nullptr)
		{
			_ptr = new char[1];
			_ptr = '\0';
		}
		else
		{
			_ptr = new char[strlen(_str) + 1];
			strcpy(_ptr, _str);
		}
	}
	bool operator>(const String&str)const
	{
		return strcmp(_ptr, str._ptr) > 0;
	}
	bool operator<(const String &str)const
	{
		return strcmp(_ptr, str._ptr) < 0;
	}
	bool operator==(const String &str)const
	{
		return strcmp(_ptr, str._ptr) == 0;
	}
	int length()const
	{
		return strlen(_ptr);
	}
	const char*c_str()const
	{
		return _ptr;
	}
	char &operator[](int index)
	{
		if (index > length() - 1 || index < 0)
			throw OUTOFRANDGE;
		return _ptr[index];
	}
private:
	char *_ptr;
	friend String operator + (const String &lhs, const String &rhs);
	friend ostream& operator<<(ostream &out, const String &str);
	friend istream& operator>>(istream &in, String &str);
};
String operator+ (const String &lhs, const String &rhs)
{
	String tmp;
	tmp._ptr = new char[strlen(lhs._ptr) + strlen(rhs._ptr) + 1];
	strcpy(tmp._ptr, lhs._ptr);
	strcat(tmp._ptr, rhs._ptr);
	return tmp;
}
ostream& operator<<(ostream &out, const String &str)
{
	out << str._ptr;
	return out;
}
istream& operator>>(istream &in, String &str)
{
	/*delete[]str._ptr;
	str._ptr = nullptr;
	char tmp[1024] = { 0 };
	in >> tmp;
	str = tmp;
	return in;*/
	delete[] str._ptr;
	str._ptr = new char[1024];
	in >> str._ptr;
	return in;
}

相关的测试代码

int main()
{
	String str1 = "aaa";
	String str2 = "bbb";
	String str3 = str1 + str2;
	String str4 = str1 + "ccc";
	String str5 = "ddd" + str4;

	cout << "str5:" << str5 << endl;
	cin >> str1; // cin >>  scanf
	cout << "str1:" << str1 << endl;

	// bool operator>(const MyString &str)
	if (str5 > str1)
	{
		cout << "str5 > str1" << endl;
	}
	int size = str1.length(); // string str1 
	for (int i = 0; i < size; ++i)
	{
		cout << str1[i]; // ar & operator[](int index)
	}
	cout << endl;
	// string => char buf[1024]
	char buf[1024] = { 0 };
	strcpy(buf, str1.c_str());
	cout << "buf:" << buf << endl;

	return 0;

	cout << str5;
	return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 C++ 中,可以使用运算符重载实现字符串的连接。具体实现如下: ```cpp #include <iostream> #include <cstring> class String { private: char* str; public: String() : str(nullptr) {} String(const char* s) { str = new char[strlen(s) + 1]; strcpy(str, s); } ~String() { if (str) delete[] str; } // 重载 + 运算符 String operator+ (const String& s) const { String res; res.str = new char[strlen(str) + strlen(s.str) + 1]; strcpy(res.str, str); strcat(res.str, s.str); return res; } // 重载 += 运算符 String& operator+= (const String& s) { char* new_str = new char[strlen(str) + strlen(s.str) + 1]; strcpy(new_str, str); strcat(new_str, s.str); if (str) delete[] str; str = new_str; return *this; } // 重载 << 运算符 friend std::ostream& operator<< (std::ostream& os, const String& s) { os << s.str; return os; } }; int main() { String s1("hello"); String s2("world"); String s3 = s1 + s2; // 使用重载 + 运算符连接字符串 std::cout << s3 << std::endl; s1 += s2; // 使用重载 += 运算符连接字符串 std::cout << s1 << std::endl; return 0; } ``` 在上述代码中,我们定义了一个 `String` 类,其中重载了 `+` 运算符和 `+=` 运算符实现字符串的连接。同时,我们还重载了 `<<` 运算符来方便输出字符串。 在 `+` 运算符实现中,我们首先申请一段新的字符串空间,将两个字符串连接起来,最后返回一个新的 `String` 对象。而在 `+=` 运算符实现中,我们首先申请一段新的字符串空间,将两个字符串连接起来,然后释放旧的字符串空间,将指针指向新的字符串空间。 在 `main` 函数中,我们演示了如何使用重载运算符来连接字符串。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值