首页 > 代码库 > string类实现(C++)

string类实现(C++)

 1 class CMyString
 2 {
 3         friend std::ostream& operator<<( std::ostream& os, const CMyString& str);
 4         private:
 5                 char* m_pData; //  私有变量保存字符串
 6         public:
 7                 CMyString( const char* str = NULL ); // 构造函数
 8                 CMyString( const CMyString& str ); // 拷贝构造函数
 9                 ~CMyString( void ); // 析构函数
10                 CMyString& operator=( const CMyString& str ); // 赋值运算符
11                 CMyString operator+( const CMyString& str ); // 字符串连接 
12                 bool operator==( const CMyString& str ); // 判断相等
13                 char operator[]( int idx ); // 数组索引
14                 int getLength(); // 返回长度
15 };
  1 CMyString::CMyString( const char* str )
  2 {
  3         if ( !str )
  4         {
  5                 this->m_pData = http://www.mamicode.com/0;
  6         }
  7         else
  8         {
  9                 this->m_pData = http://www.mamicode.com/new char[ strlen( str ) + 1 ];
 10                 strcpy( this->m_pData, str );
 11         }
 12 }
 13 
 14 CMyString::CMyString( const CMyString& str )
 15 {
 16         if ( !str.m_pData )
 17         {
 18                 this->m_pData = http://www.mamicode.com/0;
 19         }
 20         else
 21         {
 22                 this->m_pData = http://www.mamicode.com/new char[ strlen( str.m_pData ) + 1 ];
 23                 strcpy( this->m_pData, str.m_pData );
 24         }
 25 }
 26 
 27 CMyString::~CMyString( void )
 28 {
 29         if ( this->m_pData)
 30         {
 31                 delete[] this->m_pData;
 32                 this->m_pData = http://www.mamicode.com/0;
 33         }
 34 }
 35 
 36 CMyString& CMyString::operator=( const CMyString& str)
 37 {
 38         if ( this != &str )
 39         {
 40                 delete[] this->m_pData;
 41                 if ( !str.m_pData )
 42                 {
 43                         this->m_pData = http://www.mamicode.com/0;
 44                 }
 45                 else
 46                 {
 47                         this->m_pData = http://www.mamicode.com/new char[ strlen( str.m_pData ) + 1 ];
 48                         strcpy( this->m_pData, str.m_pData );
 49                 }
 50         }
 51         return *this;
 52 }
 53 
 54 CMyString CMyString::operator+( const CMyString& str )
 55 {
 56         CMyString newString;
 57         if ( !str.m_pData )
 58         {
 59                 newString = *this;
 60         }
 61         else if ( !this->m_pData )
 62         {
 63                 newString = str;
 64         }
 65         else
 66         {
 67                 newString.m_pData = http://www.mamicode.com/new char[ strlen( this->m_pData ) + strlen( str.m_pData ) + 1 ];
 68                 strcpy( newString.m_pData, this->m_pData );
 69                 strcat( newString.m_pData, str.m_pData );
 70         }
 71 
 72         return newString;
 73 
 74 }
 75 
 76 bool CMyString::operator==( const CMyString& str )
 77 {
 78         if ( strlen(this->m_pData) != strlen( str.m_pData ) )
 79         {
 80                 return false;
 81         }
 82         else
 83         {
 84                 return strcmp( this->m_pData, str.m_pData ) ? false : true;
 85         }
 86 }
 87 
 88 char CMyString::operator[]( int idx)
 89 {
 90         if ( idx > 0 && idx < strlen( this->m_pData ) )
 91         return this->m_pData[idx];
 92 }
 93 
 94 int CMyString::getLength()
 95 {
 96         return strlen(this->m_pData);
 97 }
 98 
 99 std::ostream& operator<<( std::ostream& os, const CMyString& str )
100 {
101         os<< str.m_pData;
102         return os;
103 }