Why is the destructor of the class called twice?

To add the element a copy constructor is invoked on a temporary object. After the push_back() the temporary object is destroyed – that’t the first destructor call. Then vector instance goes out of scope and destroys all the elements stored – that’s the second destructor call.

Leave a Comment