buchipper already gave you good advice. When you want to manage the lifetime of your pets correctly, consider to use std::unique_ptr<>
or std::shared_ptr<>
instead of raw pointers:
// the vector owns the pets and kills them, when they are removed
// from the vector
vector<std::unique_ptr<Mamal> > v1
// the vector has shared ownership of the pets. It only kills them,
// when noone else needs them any more
vector<std::shared_ptr<Mamal> > v2
// the vector has no ownership of the pets. It never kills them.
vector<Mamal*> v3
In the last case, someone else has to take care of the pets death, or they'l hang around in your memory as zombies. You don't want to to that to your pets, do you?
Update
Oh, i forgot to mention, that you should prefer make_shared()
and make_unique()
over new, or use emplace_back()
instead of push_back()
v1.emplace_back(new Dog{Name, Blue, Owner});
v1.push_back(make_unique<Dog>(Name, Blue, Owner))
v2.emplace_back(new Dog{Name, Blue, Owner});
v2.push_back(make_shared<Dog>(Name, Blue, Owner))