你的代码已经很完善了,但有以下几点需要注意和改进:
-
operator+
重载实现缺失: -
你在类中声明了
String operator+(const char *s);
,但在定义时没有实现该函数体。这个重载的作用是允许String
对象与const char*
类型的字符串相加。可以通过将const char*
转换为String
对象,然后调用已有的operator+(const String&)
实现: -
String String::operator+(const char *s) { return *this + String(s); // 利用已有的 String + String 重载 }
-
确保拼接后的字符串有效: 在
operator+
的实现中,你已经正确分配了result.str
的内存来存放拼接后的字符串。此时的operator+
实现没有问题。 -
检查异常安全性(可选): 在复杂项目中,可以考虑在操作前先创建新内存以确保操作的异常安全性。不过在当前情况下,操作已经是相对安全的。
-
测试链式拼接: 现在,你的
operator+(const char *s)
实现应该能支持像s + "520" + "1314"
的链式拼接。 -
String String::operator+(const char *s) { return *this + String(s); // 利用已有的 String + String 重载 }
-
String String::operator+ (const char *s) const{ //非对象拼接
- 第一个
const
:限制传入参数,防止修改传入的String
对象s
。 - 第二个
const
:限制成员函数,保证函数内不会修改当前对象(*this
)。
完整代码
//
// Created by 徐昌真 on 2024/11/11.
//
#include <iostream>
#include <cstring>
using namespace std;
class String{
private:
char *str;
size_t length;
public:
String(); //默认构造函数
String(const char *s); //传参构造函数
String(const String &s); //拷贝构造函数
~String(); //析构函数
size_t getLength() const; //获取字符长度
char operator[] (size_t index) const; //获取索引元素
String& operator= (const String &s); //使对象相等(没有构建副本的意思) //一般用于已经存在的对象
bool operator== (const String &s) const; //判断元素是否相等
bool operator!= (const String &s) const; //判断元素不等
String copy() const; //对象赋值(创建副本)
String operator+ (const String &s) const; //字符串相加
String operator+(const char *s) const; //对于直接添加字符的加法重载
friend ostream& operator<<(ostream &out, const String &s); //输出对象
};
String::String(){ //默认构造函数
str = new char[1];
length = 0;
str[0] = '\0';
}
String::String(const char *s){ //传参构造函数
length = strlen(s);
str = new char[length + 1];
strcpy(str, s);
}
String::String(const String &s) { //传参构造函数
length = s.length;
str = new char[length + 1];
strcpy(str, s.str);
}
String::~String() { //析构函数
delete[] str;
}
size_t String::getLength() const { //获取字符串长度
return length;
}
char String::operator[](size_t index) const { //返回索引元素
return str[index];
}
String &String::operator=(const String &s) { //让对象相等
if ( this != &s ){
delete[] str;
this->length = s.length;
str = new char[length + 1];
strcpy(str, s.str);
}
return *this;
}
bool String::operator==(const String &s) const{ //字符串相等
return strcmp(str, s.str) == 0;
}
bool String::operator!=(const String &s) const { //字符串不相等
return strcmp(str, s.str) != 0;
}
String String::copy() const{ //创建副本
return *this;
}
String String::operator+(const String &s) const{ //字符串拼接
String result;
result.length = s.length + length;
result.str = new char[result.length + 1]; //分配内存
strcpy(result.str, str);
strcat(result.str, s.str);
return result;
}
String String::operator+ (const char *s) const{ //非对象拼接
return *this + String(s); //调用上面那个函数+
}
ostream& operator<<(ostream& out,const String &s){ //字符输出流
out << s.str;
return out;
}
int main() {
String s("12345d");
String s1("1314");
cout << s << endl;
cout << s + "520" + "1314" << endl;
cout << s[5] << endl;
cout << (s != "12345d") << endl;
s = s + "1314";
cout << s << endl;
String x = s.copy();
cout << x << ' ' << s << endl;
x = s = s1;
cout << x << ' ' << s << ' ' << s1 << endl;
return 0;
}
输出