我們結合運算子多載知識實作string 類
在自己實作的String類中可以參考C++中string的方法
例如構造,加法,大小比較,長度,[] 等操作.
當前的MyString 類中,暫時不加入迭代器,我們將在下一節中加入迭代器的代碼.
#include <iostream>
using namespace std;
class MyString {
public:
//建構式
MyString(const char * pSource = nullptr) {
if (pSource != nullptr) {
pString = new char[strlen(pSource) + 1];
strcpy(pString, pSource);
}
else {
pString = new char[1];
pString[0] = '\0';
}
cout << "MyString建構式,物件地址="<<this << endl;
}
//拷貝構造
MyString(const MyString & _rValue) {
pString = new char[strlen(_rValue.pString) + 1];
strcpy(pString, _rValue.pString);
cout << "MyString拷貝建構式" << endl;
}
//賦值函式
MyString & operator=(const MyString & _rValue) {
if (this == &_rValue) {
return *this;
}
delete[] this->pString;
this->pString = nullptr;
char * _tpString = new char[strlen(_rValue.pString) + 1];
strcpy(_tpString, _rValue.pString);
cout << "MyString賦值函式" << endl;
}
//可編輯
char & operator[](int index) {
int len = strlen(this->pString);
if (index<0) { return pString[0]; }
else if (index>len) { return pString[index]; }
else { return pString[index]; }
}
//不可編輯
const char operator[](int index) const {
int len = strlen(this->pString);
if (index<0) { return pString[0]; }
else if (index>len) { return pString[index]; }
else { return pString[index]; }
}
bool operator>(const MyString & _rValue) const {
return strcmp(this->pString, _rValue.pString)>0;
}
bool operator<(const MyString & _rValue) const {
return strcmp(this->pString, _rValue.pString)<0;
}
bool operator==(const MyString & _rValue) const {
return strcmp(this->pString, _rValue.pString)==0;
}
int length() const {
return strlen(this->pString);
}
~MyString() {
if (this->pString != nullptr) {
delete[] this->pString;
this->pString = nullptr;
cout << "MyString解構式" << this << endl;
}
}
const char * c_str() const { return this->pString; }
private:
char * pString ;
friend MyString operator+ (const MyString & s1, const MyString &s2) ;
friend ostream & operator<<(ostream & out, const MyString &s) ;
};
MyString operator+(const MyString & s1, const MyString &s2) {
/*
方式1 這段代碼有 記憶體泄漏問題 tp 沒有釋放掉
int newLength = strlen(s1.pString) + strlen(s2.pString) + 1;
char *tp = new char[newLength + 1];//重新申請空間
strcpy(tp, s1.pString);
strcat(tp, s2.pString);
MyString s(tp);
cout << "operator+ = " << &s << endl;
return s;
*/
/*
方式2 對比方式1 效果更高
*/
MyString s;
int newLength = strlen(s1.pString) + strlen(s2.pString) + 1;
s.pString = new char[newLength + 1];//重新申請空間
strcpy(s.pString, s1.pString);
strcat(s.pString, s2.pString);
cout << "operator+ = " << &s << endl;
return s;
}
ostream & operator<<(ostream & out, const MyString &s) {
cout << s.pString << endl;
return out;
}
void test() {
MyString s1("12345");
MyString s2("6789ABC");
cout << s1 << endl;
cout << s2 << endl;
MyString s3 = s1 + s2;
cout << s3 << endl;
cout << "s3 = " << &s3 << endl;
for (int i = 0; i < s3.length(); i++) {
cout << s3[i] << endl;
}
cout <<"--------------------" << endl;
s3[0] = 'W';
for (int i = 0; i < s3.length(); i++) {
cout << s3[i] << endl;
}
const MyString s4("hello");
for (int i = 0; i < s4.length(); i++) {
cout << s4[i] << endl;
}
}
int main() {
test();
system("pause");
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/537690.html
標籤:其他
上一篇:Java——例外處理(概念理解)
