The code you've posted is correct.
The individual object pointer, polyn[0]
was created using the non-[]
version of new
, and so must be deleted using the non-[]
version of delete
-- which is what you are doing.
I'd still recommend avoiding all of this in the first place however, and using vector <unique_ptr <polyTerm> >
instead. Then all you have to do is erase
whatever elements you want, or clear
the entire vector
without having to worry about using the correct type and number of new
and delete
.
I'll demonstrate using a vector
of smart pointers here, neatly avoiding the lack of make_unique
(until C++14) by using shared_ptr
instead.
// Create array (vector)
vector <shared_ptr <polyTerm>> polyn;
// Populate array
polyn.push_back (make_shared (5.0, 1));
polyn.push_back (make_shared (2,0, 1));
// Remove 0th element from array
polyn.erase (polyn.begin());