#include<iostream>#include<cstring>#strcpyusingnamespacestd;classString{public:String(constchar*str=NULL):m_str(strcpy(newchar[strlen(str?str:"")+1],str?str:"")){}~String(){if(m_str){delete[]m_str;m_str=NULL;}}//拷贝构造String(constString&that):m_str(strcpy(newchar[strlen(that.m_str)+1],that.m_str)){}//拷贝赋值,成员变量m_str为指针为指针时,要注意深拷贝,这里为深拷贝String&operator=(constString&that){if(&that!=this){char*=newchar[strlen(that.m_str)+1];delete[]m_str;//先释放旧资源m_str=strcpy(str,that.m_str);}return*this;}friendostream&operator<<(ostream&os,constString&c){os<<c.m_str;returnos;}//c接口constchar*c_str(void)const{returnm_str;}public:char*m_str;};intmain(void){Strings1("hello,world");cout<<s1<<endl;return0;}