: _pstr(new char[5]() + 4)
cout << "String()" << endl;
: _pstr(new char[strlen(pstr) + 5]() + 4)
cout << "String(const char *)" << endl;
String(const String &rhs)
cout << "String(const String &)" << endl;
String &operator=(const String &rhs)
CharProxy(String &self, size_t idx)
char &operator=(const char &ch);
friend std::ostream &operator<<(std::ostream &os, const CharProxy &rhs);
CharProxy operator[](size_t idx)
return CharProxy(*this, idx);
char &operator[](size_t idx)
char *ptmp = new char[size() + 5]() + 4;
static char charnull = '\0';
cout << "~String()" << endl;
const char *c_str() const
return *(int *)(_pstr - 4);
friend std::ostream &operator<<(std::ostream &os, const String &rhs);
friend std::ostream &operator<<(std::ostream &os, const String::CharProxy &rhs);
std::ostream &operator<<(std::ostream &os, const String &rhs)
char &String::CharProxy::operator=(const char &ch)
if(_self.getRefCount() > 1)
char *ptmp = new char[_self.size() + 5]() + 4;
strcpy(ptmp, _self._pstr);
_self.decreaseRefCount();
return _self._pstr[_idx];
static char charnull = '\0';
std::ostream &operator<<(std::ostream &os, const String::CharProxy &rhs)
os << rhs._self._pstr[rhs._idx];
cout << "s1 = " << s1 << endl;
cout << "s2 = " << s2 << endl;
cout << "s1.getRefCount() = " << s1.getRefCount() << endl;
cout << "s2.getRefCount() = " << s2.getRefCount() << endl;
printf("s1'address : %p\n", s1.c_str());
printf("s2'address : %p\n", s2.c_str());
cout << "s3 = " << s3 << endl;
cout << "s3.getRefCount() = " << s3.getRefCount() << endl;
printf("s3'address : %p\n", s3.c_str());
cout << endl << "使用s3 = s1进行赋值操作" << endl;
cout << "s1 = " << s1 << endl;
cout << "s2 = " << s2 << endl;
cout << "s3 = " << s3 << endl;
cout << "s1.getRefCount() = " << s1.getRefCount() << endl;
cout << "s2.getRefCount() = " << s2.getRefCount() << endl;
cout << "s3.getRefCount() = " << s3.getRefCount() << endl;
printf("s1'address : %p\n", s1.c_str());
printf("s2'address : %p\n", s2.c_str());
printf("s3'address : %p\n", s3.c_str());
cout << endl << "对s3[0]执行写操作" << endl;
s3.operator[](0).operator=('H');
cout << "s1 = " << s1 << endl;
cout << "s2 = " << s2 << endl;
cout << "s3 = " << s3 << endl;
cout << "s1.getRefCount() = " << s1.getRefCount() << endl;
cout << "s2.getRefCount() = " << s2.getRefCount() << endl;
cout << "s3.getRefCount() = " << s3.getRefCount() << endl;
printf("s1'address : %p\n", s1.c_str());
printf("s2'address : %p\n", s2.c_str());
printf("s3'address : %p\n", s3.c_str());
cout << endl << "对s1[0]执行读操作" << endl;
cout << "s1[0] = " << s1[0] << endl;
cout << "s1 = " << s1 << endl;
cout << "s2 = " << s2 << endl;
cout << "s3 = " << s3 << endl;
cout << "s1.getRefCount() = " << s1.getRefCount() << endl;
cout << "s2.getRefCount() = " << s2.getRefCount() << endl;
cout << "s3.getRefCount() = " << s3.getRefCount() << endl;
printf("s1'address : %p\n", s1.c_str());
printf("s2'address : %p\n", s2.c_str());
printf("s3'address : %p\n", s3.c_str());
int main(int argc, char **argv)

cout << "String()" << endl;
: _pstr(new char[strlen(pstr) + 1]())
cout << "String(const char *)" << endl;
String(const String &rhs)
: _pstr(new char[strlen(rhs._pstr) +1]())
cout << "String(const String &)" << endl;
strcpy(_pstr, rhs._pstr);
cout << "~String()" << endl;
String &operator=(const String &rhs)
cout << "String &operator=(const String &)" << endl;
_pstr = new char[strlen(rhs._pstr) + 1]();
strcpy(_pstr, rhs._pstr);
String &operator=(const char *pstr)
cout << "String &operator=(const char *)" << endl;
String &operator+=(const String &rhs)
cout << "String &operator+=(const String &)" <
tmp._pstr = new char[strlen(_pstr) + 1]();
strcpy(tmp._pstr, _pstr);
_pstr = new char[strlen(rhs._pstr) + strlen(tmp._pstr) + 1]();
strcpy(_pstr, tmp._pstr);
strcat(_pstr, rhs._pstr);
String &operator+=(const char *pstr)
cout << "String &operator+=(const char *)" << endl;
char &operator[](std::size_t index)
static char nullchar = '\0';
const char &operator[](std::size_t index) const
static char nullchar = '\0';
const char* c_str() const
friend bool operator==(const String &, const String &);
friend bool operator!=(const String &, const String &);
friend bool operator<(const String &, const String &);
friend bool operator>(const String &, const String &);
friend bool operator<=(const String &, const String &);
friend bool operator>=(const String &, const String &);
friend std::ostream &operator<<(std::ostream &os, const String &s);
friend std::istream &operator>>(std::istream &is, String &s);
bool operator==(const String &lhs, const String &rhs)
return !strcmp(lhs._pstr, rhs._pstr);
bool operator!=(const String &lhs, const String &rhs)
return strcmp(lhs._pstr, rhs._pstr);
bool operator<(const String &lhs, const String &rhs)
return strcmp(lhs._pstr, rhs._pstr) < 0;
bool operator>(const String &lhs, const String &rhs)
return strcmp(lhs._pstr, rhs._pstr) > 0;
bool operator<=(const String &lhs, const String &rhs)
return strcmp(lhs._pstr, rhs._pstr) <= 0;
bool operator>=(const String &lhs, const String &rhs)
return strcmp(lhs._pstr, rhs._pstr) >= 0;
std::ostream &operator<<(std::ostream &os, const String &rhs)
std::istream &operator>>(std::istream &is, String &rhs)
while((ch = is.get()) != '\n')
rhs._pstr = new char[buffer.size() + 1]();
strncpy(rhs._pstr, &buffer[0], buffer.size());
String operator+(const String &lhs, const String &rhs)
cout << "String operator+(const String &, const String &)" << endl;
String operator+(const String &lhs, const char *pstr)
cout << "String operator+(const String &, const char *)"<< endl;
String operator+(const char *pstr, const String &rhs)
cout << "String operator+(const char*, const String &)" << endl;
cout << "s1 = " << s1 << endl;
cout << "s2 = " << s2 << endl;
cout << endl << "1111" << endl;
cout << "s2 = " << s2 << endl;
cout << "s2 = " << s2 << endl;
s3 += " welcome to string word";
cout << "s3 = " << s3 << endl;
int main(int argc, char **argv)



