It's correct, as long as you also have a correct copy constructor and copy-assignment operator per the Rule of Three. Note that the clear()
is redundant, since the vector's destructor will release its memory.
By why are you messing around with pointers and new
? Why not follow the Rule of Zero, and use vector<B>
, or vector<unique_ptr<B>>
if you need pointers for polymorphism? Then you shouldn't need to worry about a destructor, copy constructor or copy-assignment operator at all; and you'll get move semantics as a bonus.