1#include <iostream>2#include <cstring>3 using namespacestd;4 classString {5 Public:6String (Const Char* str =NULL) {7M_STR =New Char[Strlen (STR?STR:"")+1];8strcpy (M_STR, str. STR:"");9 }Ten~string (void) { One if(m_str) { A Delete[] m_str; -M_STR =NULL; - } the } -String (Conststring&that): - m_str (strcpy ( - New Char[Strlen (THAT.M_STR) +1], + that.m_str)) {} - /*Rookie + void operator= (const string& that) { A m_str = new Char[strlen (THAT.M_STR) +1]; at strcpy (m_str, that.m_str); - }*/ -string&operator= (Conststring&That ) { - if(&that! = This) { - /*Bird - delete[] m_str; in m_str = new Char[strlen (THAT.M_STR) +1]; - strcpy (m_str, that.m_str); to */ + /*Big Bird - char* str = the new Char[strlen (THAT.M_STR) +1]; * delete[] m_str; $ m_str = strcpy (str, that.m_str);Panax Notoginseng */ - //Veteran the String Temp (that); + swap (m_str, temp.m_str); A } the return* This; + } - Const Char* C_STR (void)Const { $ returnm_str; $ } - operator Const Char* (void)Const { - returnc_str (); the } - Private:Wuyi Char*m_str; the }; - intMain (void) { WuString S1 ("Hello, World!"); -cout << s1.c_str () <<Endl; AboutString s2 =S1; $cout << s2.c_str () <<Endl; -String S3 ("Hello, Linux!"); - Try { -S1 =S3; A } + Catch(exception&ex) { thecout << ex.what () <<Endl; - } $cout << s1.c_str () <<Endl; the Const Char* Psz =S1; thecout << S1 <<Endl; thecout << psz <<Endl; the return 0; -}
C + + implements the String class