我正在尝试为练习实现一个字符串类。这是我的所有代码:(您不需要全部阅读):
#include <iostream>
using namespace std;
// ******************************* String ************************
class String {
private:
char* char_arr;
int strlen(const char* str) {
int length;
for(length=0;*(str+length)!='\0';length++);
return length;
}
void strCopy(const char* str1, char* str2) {
int length = strlen(str1);
for(int i=0;i<length;i++)
str2[i] = str1[i];
}
public:
// --- Constructor ---
String(const char* str) {
int length = strlen(str);
char_arr = new char[strlen(str)+1];
char_arr[length] = '\0';
strCopy(str,char_arr);
}
// --- size Constructor ---
explicit String(int size) {
char_arr = new char[size+1];
char_arr[size+1] = '\0';
}
// --- Destructor ---
~String() {
delete char_arr;
}
// --- Copy Constructor ---
String(const String& rhs) {
char_arr = new char[strlen(rhs.char_arr)+1];
strCopy(rhs.char_arr,char_arr);
}
// --- copy-assignment Constructor
const String& operator=(const String& rhs) {
delete char_arr;
char_arr = new char[strlen(rhs.char_arr)+1];
strCopy(rhs.char_arr,char_arr);
}
// --- operator== ---
bool operator==(const String& rhs) {
int this_length = strlen(char_arr);
int rhs_length = strlen(rhs.char_arr);
if(this_length==rhs_length) {
bool return_value = true;
for(int i=0;i<this_length;i++) {
if(char_arr[i]!=rhs.char_arr[i]) {
return_value = false;
break;
}
}
return return_value;
}
return false;
}
// --- operator+ ---
String operator+(const String& rhs) {
int this_length = strlen(char_arr);
int rhs_length = strlen(rhs.char_arr);
String new_str(this_length+rhs_length);
strCopy(char_arr,new_str.char_arr);
for(int i=0;i<rhs_length;i++) {
new_str.char_arr[i+this_length] = rhs.char_arr[i];
}
new_str.char_arr[this_length+rhs_length] = '\0';
return new_str;
}
// --- print ---
void print() {
cout << char_arr;
}
};
// ~~~~~~~ main ~~~~~~~~~
int main() {
String s = "This is";
String s1 = " My Name";
String s2 = s+s1;
s1.print();
return 0;
}
问题在于 operator+ 重载。如果查看主函数并更改 s 和 s1 的值,程序有时会崩溃,有时不会。任何想法为什么会发生?