Pointers in a vector pointing to stack objects,

Hi all,

Not sure if this should be under the beginners forum but here we go :)

Say I have declared som objects:

Dog Dog1;
Dog Dog2;
Dog Dog3;

Then I have a vector with pointers to the dogs where I put them.
std::vector<Dog*> dogcontainer;

dogcontainer.push_back(&Dog1);
dogcontainer.push_back(&Dog2);
dogcontainer.push_back(&Dog3);

I figured its better to let the vector store pointers to the dogs Dogs with performance in mind.

The Dog1,Dog2 and Dog3 objects are automatically deleted when they go out of scope, fine all is good!

But what about my vector with pointers? Do I need to do anything with them to prevent anything bad to happen? Will they also be taken care of automatically when out of scope?
Vectors are only aware of the objects they contain. When a pointer is destructed, nothing happens with the object it points to. In other words, nothing (outside the vector) happens when a vector of pointers is destructed.
This is fine. As long as the vector is in the same scope or in a scope that is inside the scope of the objects it contains then there is no problem.
Ok great thank you both for the fast replies!
Topic archived. No new replies allowed.