I store dynamically assigned class pointers in a vector, as shown below.
vector<Test *> v;
Test *t1 = new Test;
Test *t2 = new Test;
v.push_back(t1);
v.push_back(t2);
Now, if I need to free Test objects, I have to skip the whole vector and free one by one, and then make it clear.
for(int i = 0; i<v.size(); i++)
{
delete v[i];
}
v.clear();
Is there any function in the vector for freeing all internal objects. This function should call the destructor of the Test class for each object. I understand that it is difficult for the Vector class to determine if the pointer is a local object address or dynamically allocated. But still, whenever a developer is sure that everyone is dynamically allocated, he could use the release function, if one is provided.
, , .
Test *a = new Test[2];
delete []a;
- ?