D
dwightarmyofchampions
How exactly do I delete elements of a vector in the destructor?
Suppose my vector in my class definition looks like this:
std::vector<ABC*> vec;
which means I am declaring a vector whose elements will contain
pointer to ABC objects.
....and in my constructor I have:
vec.clear(); // make sure vector is empty before populating it
for (int i = 0; i < 5; i++)
{
ABC* abcobject1 = new ABC(i);
vec.push_back(abcobject1);
}
When I go to my destructor, do I just need to pop_back() the vector
elements...
for (int i = 0; i < 5; i++)
{
vec.pop_back();
}
....or do I delete each ABC object and then pop_back its corresponding
vector pointer...
for (int i = 0; i < 5; i++)
{
delete vec; vec = 0; // or is it (*vec)???
vec.pop_back();
}
....or do I do something else? Isn't there a delete[] statement for
this sort of thing?
Suppose my vector in my class definition looks like this:
std::vector<ABC*> vec;
which means I am declaring a vector whose elements will contain
pointer to ABC objects.
....and in my constructor I have:
vec.clear(); // make sure vector is empty before populating it
for (int i = 0; i < 5; i++)
{
ABC* abcobject1 = new ABC(i);
vec.push_back(abcobject1);
}
When I go to my destructor, do I just need to pop_back() the vector
elements...
for (int i = 0; i < 5; i++)
{
vec.pop_back();
}
....or do I delete each ABC object and then pop_back its corresponding
vector pointer...
for (int i = 0; i < 5; i++)
{
delete vec; vec = 0; // or is it (*vec)???
vec.pop_back();
}
....or do I do something else? Isn't there a delete[] statement for
this sort of thing?