No; you seem to be having some trouble with ownership in general. In this declaration:
vector<Obj> a = *(new vector<Obj>());
You are allocating a new vector
, copying it, then throwing away the original. Since you no longer have a pointer to it, you cannot delete it. This is called a memory leak. Further, this:
delete &a;
Is undefined behaviour, because you’re not deleting an object allocated via new
, but rather a local. What you perhaps meant was this:
vector<Obj*> a;
// ...
That is, allocate a local vector<Obj>
named a
, whose storage is automatically reclaimed when it goes out of scope.
Now for your actual question, no, the vector
only owns the pointers, not the memory to which they point. Consider what would happen otherwise:
vector<Obj*> a;
Obj x;
a.push_back(&x);
If the vector
called delete
on all its pointer elements, then when a
were destroyed (again, automatically), x
would also be deleted. Since it was not allocated via new
, this is undefined behaviour. Luckily, this is not the case!
If you want a vector of pointers which automatically destroy their referents when the vector
is destroyed, use a vector of unique_ptr
:
vector<unique_ptr<Obj>> a;
a.push_back(unique_ptr(new Obj()));