Sunday, November 15, 2009

study notes for operator =; +; +=

I reviewed my notes and added operator +

DString& DString :: operator = (const Dstring& s){

delete[ ] this ->data;
this -> data = (char*)0;
this -> size = s.size;
this -> data = new char[this->data+1];
strcpy(this->data, s.data);
teturn *this;

}

DString& DString :: operator += (const DString& s){

char* temp = new char[this->size+s.size+1];
strcpy(temp, this->data);
strcat(temp, s.data);
this->size += s.size;
delete [ ] this -> data;
this ->data = temp;
return * this;

}

DString& DString :: operator + (const DString& s){

char* temp = new char[this->size+s.size+1];
strcpy(temp, this->data);
strcat(temp, s.data);
DString ds(temp);
delete[ ] temp;
return ds;

}

No comments:

Post a Comment