生活随笔
收集整理的這篇文章主要介紹了
C++实现String类
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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';?? }?? ?? ?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
總結
以上是生活随笔為你收集整理的C++实现String类的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。