How to delete a reference to a derived class object stored in a base class object when base class has no virtual destructor?

Viewed 43

Is it possible to delete a derived class object stored in a base class object when the base class has no virtual destructor?

BaseClass *obj = new DerivedClass();
delete obj;

The above piece of code throws a warning, which says:

delete called on object which is abstract but has non-virtual destructor

Is it possible to cast the object to a derived class object? Something like this:

delete dynamic_cast<DerivedClass *>(obj);

Will this work? If not, is there a viable way to achieve this without adding a virtual constructor?

0 Answers
Related