O
Ook
I was taught that in a copy constructor, you don't have to destroy[] your
arrays, but in an overloaded assignment operator, you have to. Example:
When do you delete[], and when do you not? Is it arbitrary, or are there
general guidelines that should be followed? I'm thinking that in the copy
constructor, you are creating a new instance of the class, and in the
assignment, you have already created the class and therefore have to
destroy[] before you new. Is this correct?
// Copy constructor
_data = new int[ _size ];
// Overloaded Assignment operator:
delete [] _data;
_data = new int[_size];
arrays, but in an overloaded assignment operator, you have to. Example:
When do you delete[], and when do you not? Is it arbitrary, or are there
general guidelines that should be followed? I'm thinking that in the copy
constructor, you are creating a new instance of the class, and in the
assignment, you have already created the class and therefore have to
destroy[] before you new. Is this correct?
// Copy constructor
_data = new int[ _size ];
// Overloaded Assignment operator:
delete [] _data;
_data = new int[_size];