Seems like a mistake. arr is a pointer so if you do just a plain assignment you end up with two pointers sharing the memory. Since you are making a new object, this seems wrong. The pointer, h.arr, could be deleted, affecting this->arr.Code:IntArray::IntArray(const IntArray& h){ lo = h.lo; hi = h.hi; first = h.first; second = h.second; len = h.len; arr = h.arr; }
One way to fix it is to make a deep copy:
Just like that. Now each new object has its own copy of arr.Code:arr = new int[h.len]; for (int i = 0; i < len; i++) { arr[i] = h.arr[i]; }
Another way to fix it is to use shared_ptr, but you have to want the semantics of that type, and I'm not sure that you do.



1Likes
LinkBack URL
About LinkBacks



