Reputation: 1229
I'm a bit confused as to how destructors are called on objects constructed through vector, but only once when I create object using pointer.
#include <iostream>
#include <vector>
#include <string>
class Student
{
std::string first;
std::string last;
int age;
public:
Student();
Student(std::string f, std::string l, int a) : first(f), last(l), age(a)
{
};
~Student()
{
cout << "Destructor\n";
}
};
int main()
{
std::vector<Student> Univ;
Univ.push_back(Student("fn1", "ln1", 1));
Univ.push_back(Student("fn2", "ln2", 2));
Univ.push_back(Student("fn3", "ln3", 3));
return 0;
}
When I push back one time, I get 2 calls to destructor. 2 push backs, I get 5 calls to destructor. 3 push backs, I get 9 calls to destructor.
Normally if I do something like this,
Student * Univ = new Student("fn1", "ln1", 1);
delete Univ;
I get just one call to destructor.
Why is this?
Upvotes: 4
Views: 1490
Reputation: 15824
When I push back one time, I get 2 calls to destructor. 2 push backs, I get 5 calls to destructor. 3 push backs, I get 9 calls to destructor.
Before C++11, when you push back an object to a std::vector
system copy the object to the container. So if you do one push back system created 2 objects, so system has to call 2 times destructor to clean up.
Since C++11, if you define move copy constructor for your calls, std::vector use move semantics in place of copy constructor and move the object to vector instead copy the object. So system will generate only one destructor call. Following link will give you better idea http://en.cppreference.com/w/cpp/language/rule_of_three
Upvotes: 1
Reputation: 1212
when you do push_back
and the vector don't have enough memory to store the new elements it has to resize, each time the vector resize it calls the destructor of the old elements. Try this:
std::vector<Student> Univ(3);
Univ.push_back(Student("fn1", "ln1", 1));
Univ.push_back(Student("fn2", "ln2", 2));
Univ.push_back(Student("fn3", "ln3", 3));
Besides that, you are creating temporary objects to pass to push_back
, that's why the extra destructor calls.
Upvotes: 1
Reputation: 146
You are creating a temporary Student
which you pass to push_back
, which copies it into the vector. The upshot is that you use two copies of each Student
instance, and the destructor of each is called. Additionally, when you add more Student
s and the vector resizes, the existing contents are copied to the newly allocated memory, resulting in even more destructor calls.
If you have C++11 features available you may want to look into move semantics.
Upvotes: 2