| | | | | |

Assignment Operator 3

  • Second Attempt: take care of old memory allocation
  • IntArray& IntArray::operator =(const IntArray& b)
    {
      delete [] data_;
      size_ = b.size_;
      data_ = new int [size_];
      // check for failed allocation
      for (size_t i = 0; i < size_; ++i)
        data_[i] = b.data_[i];
      return *this;
    }
    
  • Problem: self-assignment

| | Top of Page | 6. C++ Classes Part 2: Advanced Features - 8 of 22