【1】应用示例
1 #include <vector> 2 #include <cassert> 3 #include <string> 4 #include <iostream> 5 using namespace std; 6 7 class String 8 { 9 public: 10 String(); 11 String(int n, char c); 12 String(const char* source); 13 String(const String& s); 14 String(String&& s); 15 String& operator=(char* s); 16 String& operator=(const String& s); 17 String& operator=(String&& s); 18 ~String(); 19 20 char& operator[](int i); 21 const char& operator[](int i) const; 22 String& operator+=(const String& s); 23 String& operator+=(const char* s); 24 25 friend ostream& operator<<(ostream& out, String& s); 26 friend ostream& operator<<(ostream& out, const String& s); 27 friend istream& operator>>(istream& in, String& s); 28 29 friend bool operator<(const String& left, const String& right); 30 friend bool operator>(const String& left, const String& right); 31 friend bool operator==(const String& left, const String& right); 32 friend bool operator!=(const String& left, const String& right); 33 34 int getSize() const; 35 void setSize(int nSize); 36 char* getData() const; 37 void setData(char* pData); 38 39 private: 40 void init(); 41 42 private: 43 int m_size; 44 char* m_data; 45 }; 46 47 void String::init() 48 { 49 m_data = new char[1]; 50 assert(m_data != NULL); 51 *m_data = '