今日任务:C++运算符重载实现的过程,代码
//+运算符
const myString myString::operator+(const myString &s)const{
myString S;
delete []S.str;
S.size = size + s.size;
S.str = new char[S.size+1];
strcpy(S.str,str);
strcat(S.str,s.str);
return S;
}
//+=运算符
myString &myString::operator+=(const myString &s){
size += s.size;
char *p = str;
delete []str;
str = new char[size+1];
strcpy(str,p);
strcat(str,s.str);
return *this;
}
//>运算符
bool myString::operator>(const myString &s)const{
return strcmp(str,s.str) > 0;
}
//[]运算符
char myString::operator[](int index){
return *(str+index);
}