Automatic storage duration interacting with other objects

152 views Asked by At

In a function if I have a vector passed as a parameter, and I have an object in the function which was created with automatic storage duration, if that object is pushed onto the vector(parameter), will that object not be destroyed until the vector is?

2

There are 2 answers

0
Kerrek SB On

You cannot "push that object" onto a vector. You're pushing a copy of the object onto the vector (unless you have something arcane like a vector of reference wrappers). So everything is fine.

(There are of course other ways to shoot yourself in the foot with poorly designed classes that don't properly manage ownership of further, dynamic objects, but that's a general problem not specific to your question.)

0
jwismar On

std::vector -- and all of the standard containers -- store copies of the objects. It's one of the requirements for objects in standard containers that they be copy-constructable.