Why is this object not destroyed?

I have an age trying to understand why this is happening.

struct Element { Element(int number) : number_ID(number) { std::cout << "Element number " << number_ID << " is being constructed\n"; } ~Element() { std::cout << "Element number " << number_ID << " is being destroyed\n"; } int number_ID; }; void createVector() { std::vector<Element> objArr; objArr.reserve(10); // So it doesn't have to reallocate objArr.emplace_back(1); objArr.emplace_back(2); objArr.emplace_back(3); objArr.emplace_back(4); objArr.erase(objArr.begin()); // When I erase the first element only element 4 has destructor called } int main() { createVector(); system("pause"); } 

I get:

 Element number 1 is being constructed Element number 2 is being constructed Element number 3 is being constructed Element number 4 is being constructed //The following called when doing objArr.erase(objArr.begin()); Element number 4 is being destroyed //The following called after createVector() function exits Element number 2 is being destroyed Element number 3 is being destroyed Element number 4 is being destroyed 

Is the destructor for element 1 never called? At first I did not know why the destructor of element number 4 would be called when the first element was erased, and then I thought that when it changes its members, perhaps the destructor should be called. But the documentation says that all members are removed after the remote transfer, and destructors 2 and 3 are not called. I'm really confused.

EDIT: Well, if this is an optimization step, then the documentation is incorrect because:

Removes from the vector either one element (position) or a range of elements ([first, last)).

This effectively reduces the size of the container by the number of items removed that are destroyed.

It does not destroy then.

+7
c ++ vector destructor
source share
1 answer

vector tries to maintain performance.

What erase does is it copies elements 2, 3 and 4 to one element, overwriting the element containing 1. So, the first element now contains 2, etc. Then it destroys the last element, which is copy 4.

This erases the first element, but not quite in the way you thought. It erases the contents of the first element, but not the object itself.

Depending on the type that you store in vector , this will be less expensive than destroying the first element, only to restore a copy of the second element to the storage of the first.

Well, if this is an optimization step, then the documentation is incorrect, because:

...

It does not destroy then.

The documentation is correct, and it destroys them.

Your objects are the values ​​that they contain. You stated that your classes were copyable, so vector can copy them. This means that it is absolutely true for two copies to have the same value. vector uses this.

If you made your classes not copied, but intransitive, then they would work correctly. But your move constructor would also have to nullify the moved object, so that none of the two instances would have the same value.

+14
source share

All Articles