1 /************************************************************************* 2 > File Name: mystring.h 3 > Author: lukey 4 > Mail: lukey123@foxmail.com 5 > Created Time: Wed 17 Jun 2015 08:50:49 PM CST 6 ************************************************************************/ 7 8 #ifndef __MYSTRING__ 9 #define __MYSTRING__ 10 11 class String 12 { 13 public: 14 String(); 15 String(const char *);//有参构造函数 16 String(const String & rhs); //复制构造 17 ~String(); 18 19 String & operator=(const String & rhs);//赋值运算符的两种情况 20 String & operator=(const char *str); 21 22 String & operator+=(const String & rhs); 23 String & operator+=(const char * str); 24 25 char & operator[](std::size_t index); 26 const char & operator[](std::size_t index) const; 27 28 std::size_t size() const; 29 const char* c_str() const; 30 void debug(); 31 32 //String 类和char相加的几个情况 33 friend String operator+(const String & s1, const String & s2); 34 friend String operator+(const String &, const char *); 35 friend String operator+(const char *, const String &); 36 37 friend bool operator==(const String &, const String &); 38 friend bool operator!=(const String &, const String &); 39 40 friend bool operator<(const String &, const String &); 41 friend bool operator>(const String &, const String &); 42 friend bool operator<=(const String &, const String &); 43 friend bool operator>=(const String &, const String &); 44 45 friend std::ostream & operator<<(std::ostream & os, const String &s); 46 friend std::istream & operator>>(std::istream & is, String & s); 47 48 private: 49 char *pstr_; 50 }; 51 52 #endif
1 /************************************************************************* 2 > File Name: mystring.cc 3 > Author: lukey 4 > Mail: lukey123@foxmail.com 5 > Created Time: Wed 17 Jun 2015 09:18:55 PM CST 6 ************************************************************************/ 7 8 #include<iostream> 9 #include<cstring> 10 #include<stdlib.h> 11 #include"mystring.h" 12 using namespace std; 13 #if 0 14 class String 15 { 16 public: 17 private: 18 char *pstr_; 19 }; 20 #endif 21 22 //构造函数 23 String::String() 24 { 25 std::cout << "String()" << std::endl; 26 pstr_ = new char[1];//new 已经初始化了 27 } 28 String::String(const char *str)//有参构造函数 29 { 30 std::cout << "String(const char * str)" << std::endl; 31 pstr_ = new char[strlen(str)+1]; 32 strcpy(pstr_, str); 33 } 34 String::String(const String & rhs) //复制构造,考虑自复制情况? 35 { 36 std::cout << "String(const String & rhs)" << std::endl; 37 pstr_ = new char[strlen(rhs.pstr_) + 1]; 38 strcpy(pstr_, rhs.pstr_); 39 } 40 String::~String() 41 { 42 std::cout << "~String()" << std::endl; 43 delete []pstr_; 44 } 45 46 String & String::operator=(const String & rhs)//赋值运算符的两种情况,考虑自赋值情况 47 { 48 std::cout << "String & operator=(const String & rhs)" << std::endl; 49 if(this == &rhs) 50 return *this; 51 delete []pstr_; 52 pstr_ = new char[strlen(rhs.pstr_) + 1]; 53 strcpy(pstr_, rhs.pstr_); 54 return *this; 55 } 56 String & String::operator=(const char *str) 57 { 58 std::cout << "String & operator=(const char *str)" << std::endl; 59 pstr_ = new char[strlen(str) + 1]; 60 strcpy(pstr_, str); 61 return *this; 62 } 63 64 String & String::operator+=(const String & rhs) //rhs连接到pstr_后面 65 { 66 std::cout << "operator+=(const String & rhs)" << std::endl; 67 int len = strlen(rhs.pstr_) + strlen(pstr_); 68 pstr_ = (char *)realloc(pstr_, len + 1); 69 strcat(pstr_, rhs.pstr_); 70 return *this; 71 } 72 String & String::operator+=(const char * str) 73 { 74 std::cout << "operator+=(const char * str)" << std::endl; 75 int len = strlen(str) + strlen(pstr_); 76 pstr_ = (char *)realloc(pstr_, len + 1); 77 strcat(pstr_, str); 78 return *this; 79 } 80 81 //下标运算符,非常量,可以修改值 82 char & String::operator[](std::size_t index) 83 { 84 return pstr_[index]; 85 } 86 87 //常量对象取下标,不能为其赋值 88 const char & String::operator[](std::size_t index) const 89 { 90 return pstr_[index]; 91 } 92 93 //字符串容量 94 std::size_t String::size() const 95 { 96 return strlen(pstr_); 97 } 98 99 //转换成c类型字符串,以' '结尾 100 const char* String::c_str() const 101 { 102 int len = strlen(pstr_); 103 104 pstr_[len + 1] = '