samedi 30 juin 2018

I need to check if thrown exception from operator=?

given the following code:

template <class T>
class A {
     T* arr;
     int size;
public:
A(int size) : arr(new T[size]) , size(size) {
}
//..

A& operator=(const A& a){
     if(this == &a){
          return *this;
     }
     this->size = a.size;
     T* ar=new T[a.size];
     for(int i=0 ; i<size ; i++){
          ar[i]=a.arr[i]; // I need to do it at "try-catch" ?
     }
     this->arr=ar;
     return *this;
}
     //...
};

While I copies the elements from the given array, I need to do it at try-catch or no? It's a good idea or not?

Aucun commentaire:

Enregistrer un commentaire