for ( int i = 0; i < countVehicle; ++i)
{
delete temp[i]; // assert destructor is virtual!
}
delete[] temp;
Make sure destructor in base class, Vehicle
, is declared virtual so the correct destructor is called when deleting objects via base class pointer, as it take place in example above.
class Vehicle {
public:
//...
virtual ~Vehicle() {}
};
However you should consider using a std::vector
of smart pointers, e.g
{
std::vector< boost::shared_ptr<Vehicle> > v;
v.push_back( boost::shared_ptr<Vehicle>( new Car()));
v[0]->run(); // or whatever your Vehicle can do
//...
} // << When v goes out of scope the memory is automatically deallocated.
This will ease memory management.