c++ - 我的自定义字符串类 operator+ 有什么问题?
问题描述
我正在尝试制作自己的 String 类,但 operator+ 有问题。
当我将 + 用作“ s1 = s2 + s3 ”时,它可以工作。
但万一只使用“s1 + s1”或“s2 + s3”不起作用。
当我运行它时,在 delete[] str_ 处的“init”函数上出现 0xC0000005 错误;线。
我想知道我的代码有什么问题...
这是我的字符串.h
class String
{
private:
int len_;
int capa_;
char* str_;
public:
String(const String& str);
String(const char* s = "");
~String();
const char* Print(bool show = true);
int size();
int length();
int capacity();
String& assign(const String& str);
String& assign(const char* s);
String& operator=(const String& str);
String& operator=(const char* s);
String& append(const String& str);
String& append(const char* s);
String& operator+=(const String& str);
String& operator+=(const char* s);
String operator+(const String& str);
String operator+(const char* s);
char& operator[](int index);
void shrink_to_fit();
friend std::ostream& operator<<(std::ostream& os, const String& str);
private:
void init(const char* s);
String plus(const char* s);
};
这是我的 string.cpp
void String::init(const char* s)
{
if (str_ == s) {}
else
{
delete[] str_;
}
len_ = strlen(s);
capa_ = len_ + 1;
str_ = new char[capa_];
strcpy_s(str_, capa_, s);
}
String::String(const String& str)
{
init(str.str_);
}
String::String(const char* s)
{
init(s);
}
String::~String()
{
delete[] str_;
}
int String::size()
{
return this->len_;
}
int String::length()
{
return strlen(this->str_);
}
int String::capacity()
{
return this->capa_;
}
String& String::assign(const String& str)
{
assign(str.str_);
return *this;
}
String& String::assign(const char* s)
{
init(s);
return *this;
}
String& String::operator=(const String& str)
{
assign(str.str_);
return *this;
}
String& String::operator=(const char* s)
{
assign(s);
return *this;
}
String& String::append(const String& str)
{
append(str.str_);
return *this;
}
String& String::append(const char* s)
{
len_ = this->len_ + strlen(s);
capa_ = len_ + 1;
char* temp = new char[capa_];
strcpy_s(temp, capa_, this->str_);
if (str_ == s) {}
else
{
delete[] str_;
}
strcat_s(temp, capa_, s);
str_ = new char[capa_];
strcpy_s(str_, capa_, temp);
delete[] temp;
return *this;
}
String& String::operator+=(const String& str)
{
append(str);
return *this;
}
String& String::operator+=(const char* s)
{
append(s);
return *this;
}
char& String::operator[](int index)
{
int index_;
if (index < 0)
{
index_ = 0;
}
else if (index > this->len_)
{
index_ = this->len_ - 1;
}
else
{
index_ = index;
}
return str_[index_];
}
String String::plus(const char* s)
{
int tempcapa = this->len_ + strlen(s) + 1;
char* temp = new char[tempcapa];
strcpy_s(temp, tempcapa, this->str_);
strcat_s(temp, tempcapa, s);
return temp;
}
String String::operator+(const String& str)
{
return plus(str.str_);
}
String String::operator+(const char* s)
{
return plus(s);
}
std::ostream& operator<<(std::ostream& os, const String& str)
{
os << str.str_;
return os;
}
void String::shrink_to_fit()
{
if (this->capa_ > this->len_ + 1)
{
char* temp = new char[len_ + 1];
strcpy_s(temp, len_ + 1, this->str_);
delete[] this->str_;
this->str_ = new char[len_ + 1];
strcpy_s(this->str_, len_ + 1, temp);
delete[] temp;
this->capa_ = len_ + 1;
}
}
const char* String::Print(bool show)
{
if (show == true)
std::cout << str_;
return str_;
}
解决方案
推荐阅读
- javascript - 反应原生项目卡在:unimodules-react-native-adapter:generateExpoModulesPackageList
- caching - 如何将搜索数据存储在redis db中进行缓存以获得最大的性能提升
- amazon-cloudwatch - 如何使用 CloudWatch Logs Insights 从 JSON 消息中的数组中提取数据?
- python - yolov5 不训练模型
- python - 如何一次用一组图像而不是一张一张地为模型提供图像?
- typescript - 没有导入的 TypeScript / Cypress 全局枚举
- angular - HTML页面上的http API请求Angular 12为空
- powershell - 从文件中获取内容并执行 dig 命令
- java - 如何在 Android 11 或 Android Q 中保存视频文件
- python - ValueError:层模型需要 5 个输入,但它收到了 3035 个输入张量