StringBad&StringBad::operator=(constStringBad&st){if(this==&st)return*this;// 【2】delete[]str;// 【1】free old stringlen=st.len;str=newchar[len+1];// get space for new stringstd::strcpy(str,st.str);// copy the stringreturn*this;// return reference to invoking object}//1) code首先进行自我检查,看目标地址与接受对象地址(this)是否相同//2) 如果地址不同,函数将释放str指向的内存:这是因为稍后将把一个新字符串的地址赋给str,如果不先使用delete,则上述字符串将遗留在内存中//3) 为新字符串分配足够的内存空间 + 字符串复制进新的内存单元//4)return *this;
// string1.cpp -- String class methods#include<cstring> // string.h for some#include"string1.h" // includes <iostream>usingstd::cin;usingstd::cout;// initializing static class memberintString::num_strings=0;// static methodintString::HowMany(){returnnum_strings;}// class methodsString::String(constchar*s)// construct String from C string 初始化构造函数{len=std::strlen(s);// set sizestr=newchar[len+1];// allot storagestd::strcpy(str,s);// initialize pointernum_strings++;// set object count}String::String()// default constructor{len=4;str=newchar[1];str[0]='\0';// default stringnum_strings++;}String::String(constString&st)// 复制构造函数{num_strings++;// handle static member updatelen=st.len;// same lengthstr=newchar[len+1];// allot spacestd::strcpy(str,st.str);// copy string to new location}String::~String()// necessary destructor{--num_strings;// requireddelete[]str;// required}// overloaded operator methods // assign a String to a StringString&String::operator=(constString&st){if(this==&st)return*this;delete[]str;len=st.len;str=newchar[len+1];std::strcpy(str,st.str);return*this;}// assign a C string to a StringString&String::operator=(constchar*s){delete[]str;len=std::strlen(s);str=newchar[len+1];std::strcpy(str,s);return*this;}// read-write char access for non-const Stringchar&String::operator[](inti){returnstr[i];}// read-only char access for const Stringconstchar&String::operator[](inti)const{returnstr[i];}// overloaded operator friendsbooloperator<(constString&st1,constString&st2){return(std::strcmp(st1.str,st2.str)<0);}booloperator>(constString&st1,constString&st2){returnst2<st1;}booloperator==(constString&st1,constString&st2){return(std::strcmp(st1.str,st2.str)==0);}// simple String outputostream&operator<<(ostream&os,constString&st){os<<st.str;returnos;}// quick and dirty String inputistream&operator>>(istream&is,String&st){chartemp[String::CINLIM];is.get(temp,String::CINLIM);if(is)st=temp;while(is&&is.get()!='\n')continue;returnis;}
初始化构造函数:String::String(constchar*s)// construct String from C string 初始化构造函数{len=std::strlen(s);// set sizestr=newchar[len+1];// allot storagestd::strcpy(str,s);// initialize pointernum_strings++;// set object count}----------------------------------------------------------------------------------------------------复制构造函数:String::String(constString&st)// 复制构造函数{num_strings++;// handle static member updatelen=st.len;// same lengthstr=newchar[len+1];// allot spacestd::strcpy(str,st.str);// copy string to new location}
// assign a String to a StringString&String::operator=(constString&st){if(this==&st)return*this;delete[]str;len=st.len;str=newchar[len+1];std::strcpy(str,st.str);return*this;}---------------------------------------------------------// assign a C string to a StringString&String::operator=(constchar*s){delete[]str;len=std::strlen(s);str=newchar[len+1];std::strcpy(str,s);return*this;}