When push_back method of vector is called the previous object in the vector is getting destroyed what might be the reason for this.
template<typename type> void SomeList<type>::AddElement(type &inObject)
{
pList.push_back(inObject);// pList is member of my class Vector SomeList
}
The vector doesn’t destroy the object. It replaces it.
For example:
That means the assignment operator (A
& A::operator=( const A&)) will be called formyVector[0]. There is no destruction there.The destruction is done when the vector itself is destructed, or when the memory is reallocated (in this case, the copy constructor is also used, to copy the objects from the old location to the new location, before destroying the old ones).
Later edit
In push_back case, the destruction must be determined by a reallocation.