lundi 20 juillet 2015

How to handle an exception thrown by new in C++?

I have a class with assignment operator as below.

char *buff;
myString& operator= ( const myString& other )
{
  cout << "  myString::operator=\n";
  if( this != &other ){      
    cout<<"my string ="<<endl;
    delete [] buff;              
    length = other.length;      
    buff = new char[length];
    my_strncpy( buff, other.buff, length );
  }
  return *this;   
}       

I am deleting memory for buff and allocating with length of new string. How can I handle any exception that happen during allocating memory with new. How can I restore the value of buff to old values incase of exception.

Aucun commentaire:

Enregistrer un commentaire