0

May someone please explain why does the current vector has to be emptied in the example below (in the while loop)? What happens if the current vector is already empty? May you also give a simple example of how you would use the overloaded operator? This is under a coplien form.

Thanks for clearing those things up.

Type& Type::operator =(const Type& p_vector2)
    {
        if (this != &p_vector2)
        {

            int i = 0;
            int j = vector1.size();
            while (i < j)
            {
                delete vector1[0];
                this->vector1.erase(vector1.begin());
                i++;
            }

            for (unsigned int i=0; i < p_vector2.vector1.size(); i++)
            {
                this->vector1.push_back(vector2.vector1[i][0].clone());
            }
        }

        return *this;
    }
A.P.
  • 461
  • 2
  • 8
  • 17
  • 1
    To see some background, and for another way of doing it, consider the [copy and swap idiom](http://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom) – sj0h May 15 '14 at 02:31

1 Answers1

3

May someone please explain why does the current vector has to be emptied in the example below (in the while loop)?

Because the author, for whatever reason, decided to store pointers to dynamically allocated memory. It is now his (your) responsibility to deallocate those objects as the vector cannot do it. This is probably a bad idea, but who knows, perhaps there is a good reason for it.

I can't explain why he doesn't just deallocate and then call clear after the loop.

What happens if the current vector is already empty?

Then the loop would never execute because i and j would be 0, and 0 is not less than 0.

May you also give a simple example of how you would use the overloaded operator?

Type t1;
Type t2;
t2 = t1;
Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • That does not invoke `operator=` – Yakk - Adam Nevraumont May 15 '14 at 02:23
  • @Yakk: Derp, fixed, not sure why I wrote that. – Ed S. May 15 '14 at 02:24
  • stack overflow bug prevents me from undoing the downvote: apparently if someone downvotes during the edit window, and you edit afterwards, the downvote cannot be rescinded. Sigh. – Yakk - Adam Nevraumont May 15 '14 at 02:33
  • But let's say, like in the three lines of example you gave, you want to put content of t1 in t2. Then why do we have to clear t1? – A.P. May 15 '14 at 02:41
  • @Alex: Because t1 is taking on the contents of t2. If the vector did not contain pointers to dynamically allocated memory a simple `clear` and `std::copy` would do – Ed S. May 15 '14 at 02:58
  • "I can't explain why he doesn't just deallocate and then call clear after the loop." - it is undefined behaviour to read the value of a pointer that has been deleted, so strictly speaking you should remove the value from the vector, and then delete it -- although he doesn't actually do that. (On typical systems this isn't an issue as they do not trap upon reading invalid or indeterminate values). – M.M May 15 '14 at 04:34
  • If you first iterate through the vector, with just `delete vector[i]`, and then clear the vector, you won't be dereferencing a pointer that has been deleted. In terms of exception safety, you shouldn't be throwing exceptions out of a destructor anyhow. – sj0h May 15 '14 at 04:53