are copy ctors of STL classes deep?

if I have a collection containing 3 Accounts: vector<Account*> m3_accounts;

and assign it to another collection: vector<Account*> m5_accounts;

like this: m5_accounts = m3_accounts;

Is that enough? (I think it is) Or do i have to traverse m3_accounts, allocate new Account objects and push_back() each to m5_account?

thanks, joe
Last edited on
As written, your vector(s) do not contain account objects, they contain pointers to account objects ( account* ).

So assigning vector a = vector B will only copy pointers from one to the other.
The pointers in both vectors will point to the same objects.
Last edited on
wow. I was afraid of that. I thought maybe with as "intelligent" as container classes are, they could account for that and allocate new memory for each Account on their own. But ok, I will do it.

thanks
You could make a vector of boost::shared_ptr<Account>, depending upon your requirements.

Trust me, the std::vector *is* intelligent but not clairvoyant. Vector does perform a deep copy of itself but it is only a container. Remember that. It invokes the copy constructor of each element it contains. In your case, the elements are pointers. vector has no way of knowing what each of the elements are or how they should be copied. Therefore it is a requirement that each type used to build a vector is copyable on its own.
Topic archived. No new replies allowed.