http://blog.csdn.net/randyjiawenjie/article/details/6709539
C++實現String類,還沒有完成,待繼續。
有以下注意的點:
(1)賦值操作符返回的是一個MyString&,而重載的+返回的是一個MyString。其中的原因參看《effective c++》,主要是返回引用的時候,必須返回必須在此函數之前存在的引用,因為引用是一個名字,“在我們使用之前,必須想想他代表了那個名字“。如果返回了一個局部對象的引用,那么這個函數結束后,這個引用就會指向一個不存在的對象,顯然,這是走上了行為未定義的快車道。
[cpp]?view plain?copy
- #?include?<iostream>??
- #?include?<memory>??
- #?include?<cstring>??
- using?namespace?std;??
- class?MyString?{??
- private:??
- ????char?*m_data;??
- public:??
- ????MyString();??
- ????MyString(const?char*?ptr);??
- ????MyString(const?MyString&?rhs);??
- ????~MyString();??
- ????MyString&?operator=(const?MyString&?rhs);??
- ????MyString?operator+(const?MyString&?rhs);??
- ????char?operator[](const?unsigned?int?index);??
- ????bool?operator==(const?MyString&?rhs);??
- ????friend?ostream&?operator<<(ostream&?output,?const?MyString?&rhs);??
- };??
- //默認的構造函數??
- ?MyString::MyString()?{??
- ????m_data?=?new?char[1];??
- ????*m_data?=?'\0';??
- }??
- //使用const?char*?來初始化??
- ?MyString::MyString(const?char*?ptr)?{??
- ????if?(NULL?==?ptr)?{??
- ????????m_data?=?new?char[1];??
- ????????*m_data?=?'\0';??
- ????}?else?{??
- ????????int?len?=?strlen(ptr);??
- ????????m_data?=?new?char[len?+?1];??
- ????????strcpy(m_data,?ptr);??
- ????}??
- }??
- //拷貝構造函數??
- ?MyString::MyString(const?MyString&?rhs)?{??
- ????int?len?=?strlen(rhs.m_data);??
- ????m_data?=?new?char[len?+?1];??
- ????strcpy(m_data,?rhs.m_data);??
- }??
- bool?MyString::operator?==(const?MyString&?rhs)?{??
- ????int?result?=?strcmp(m_data,?rhs.m_data);??
- ????if?(0?==?result)??
- ????????return?true;??
- ????else??
- ????????return?false;??
- }??
- //賦值操作符??
- ?MyString&?MyString::operator?=(const?MyString&?rhs)?{??
- ????if?(this?!=?&rhs)?{??
- ????????delete[]?m_data;??
- ????????m_data?=?new?char[strlen(rhs.m_data)?+?1];??
- ????????strcpy(m_data,?rhs.m_data);??
- ????}??
- ????return?*this;??
- }??
- //重載運算符+??
- ?MyString?MyString::operator+(const?MyString?&rhs)?{??
- ????MyString?newString;??
- ????if?(!rhs.m_data)??
- ????????newString?=?*this;??
- ????else?if?(!m_data)??
- ????????newString?=?rhs;??
- ????else?{??
- ????????newString.m_data?=?new?char[strlen(m_data)?+?strlen(rhs.m_data)?+?1];??
- ????????strcpy(newString.m_data,?m_data);??
- ????????strcat(newString.m_data,?rhs.m_data);??
- ????}??
- ????return?newString;??
- }??
- //重載下標運算符??
- ?char?MyString::operator?[](const?unsigned?int?index)?{??
- ????return?m_data[index];??
- }??
- //析構函數??
- ?MyString::~MyString()?{??
- ????delete[]?m_data;??
- }??
- //重載<<??
- ?ostream&?operator<<(ostream&?output,?const?MyString?&rhs)?{??
- ????output?<<?rhs.m_data;??
- ????return?output;??
- }??
- int?main()?{??
- ????const?char*?p?=?"hello,world";??
- ????MyString?s0?=?"hello,world";??
- ????MyString?s1(p);??
- ????MyString?s2?=?s1;??
- ????MyString?s3;??
- ????s3?=?s1;??
- ????MyString?s4?=?s3?+?s1;??
- ????bool?flag(s1?==?s2);??
- ????cout?<<?s0?<<?endl;??
- ????cout?<<?s1?<<?endl;??
- ????cout?<<?s2?<<?endl;??
- ????cout?<<?s3?<<?endl;??
- ????cout?<<?flag?<<?endl;??
- ????char?result?=?s3[1];??
- ????cout?<<?result?<<?endl;??
- ????cout?<<?s4?<<?endl;??
- ????return?0;??
- }??
運行結果:
hello,world
hello,world
hello,world
hello,world
1
e
hello,worldhello,world