string class assignment operator overloading in c++ -
i defined own string class, mystring. works until assign 1 object other overloaded operator=. know problem don't know how fix it. helps?
class mystring{ public: mystring( const mystring *strini ); mystring( const char *str); ~mystring(); mystring& operator=( const mystring &str ); private: char *str; } mystring::mystring( const mystring *strini ){ this->str = new char[strlen(strini->str)+1]; strcpy(this->str,strini->str) ; }; mystring::mystring( const char *str){ this->str = new char[ strlen(str) + 1 ]; strcpy(this->str , str); }; mystring::~mystring(){ delete [] this->str ; cout << "successfully deleted..." << endl; }; mystring& mystring::operator=( const mystring &str ){ // temp obj holding rhs mystring strtmp(str); // temp char pointer holding rhs char *ctmp = strtmp.str; // temp obj holding this, later release memory strtmp.str = this->str ; // holding rhs; assignment done. this->str = ctmp; return *this ; }; int main(){ { // line 1 mystring mystr1("string #1"); // line 2 mystring mystr2("string #2"); // line 3 mystr1 = mystr2; // line 4 } // line 5 return 0; } the problem of code is: @ line4, after assignment pointer in 2 objects mystr1 , mystr2 both point same string "string #2". when program jump out of brackets @ line 5, destructors automatically called sequence: mystr2 , mystr1. after mystr2 destructed, memory of "string #2" has been released. when destructor of mystr1 trying release non-existing memory, program crashed.
anybody can me fix overloading member function. when assign mystr1 = mystr2, can create new string instead of making 2 pointers pointing same string.
thanks lot!!
updates further questions...... thank tons!!
actually, using copy-and-swap in overloading function. based on @mateusz kołodziejski 's advice, modified it:
mystring& mystring::operator=( const mystring &rhs ){ if( != &rhs ){ // copy using constructor mystring strtmp(rhs) ; // swap char *ctmp = strtmp.str; // strtmp destructed, memory in released strtmp.str = this->str ; // size of rhs const int str_size = strlen(rhs.str); this->str = new char[str_size+1]; copy(rhs.str,rhs.str+str_size,this->str); } return *this ; }; when destructors called, no crash. if printout member function added, there seems problem:
void mystring::printout(){ int str_size = strlen(this->str); cout << "string size: " << str_size << endl ; for( int i=0;i<str_size;i++ ){ cout << *(this->str + i); } } in main function:
int main(){ { mystring mystr1("string #1"); mystring mystr2("string #2"); mystr1.printout(); mystr2.printout(); mystr1 = mystr2; cout << "after assignment: " << endl; mystr1.printout(); mystr2.printout(); } return 0; } the results are:
string #1 string #2 after assignment... string #2═²²²² string #2 seems mystr1 not normal...
anybody can explain me?
thank tons!!
you have fix operator=() implementation.
#include <algorithm> mystring& mystring::operator=( const mystring &rhs ) // (1) { if (this != &rhs) // (2) { delete[] this->str; // (3) this->str = null; const int str_length = strlen(rhs.str); this->str = new char[str_length + 1]; this->str[str_length] = '\0'; std::copy(rhs.str, rhs.str + str_length, this->str); // (4) } return *this; } 1) use "rhs" (right-hand-side) instead of "str" variable name avoid ambiguity.
2) check if object not being assigned itself.
3) release old allocated memory before allocating new.
4) copy on contents of rhs this->str, instead of redirecting pointers.
edit:
added this->str = null; avoid double deletion on possible exception new, , later object deconstruction.
this naïve implementation - have aware new can throw exception. copy-swap idiom better here suggested @nyarlathotep , described here: copy-and-swap.
Comments
Post a Comment