运算符重载实现string类
大约 2 分钟
运算符重载实现string类
注意事项
等号赋值的四步走
- 防止自己给自己赋值
- 释放之前所占的外部资源
- 申请新的空间然后将数据拷贝进去
- 返回*this
析构函数执行后记得要把指针变量指向nullptr,避免野指针的出现。
String str1; str.~String(); str.~String();
str在第二次调用析构函数后。
实现代码
#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;
}